Cookies - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Cookies

Description:

The browser (document object) stores one cookie per page ... Use to share information with other pages from the same domain ... document.cookie = 'name1 ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 11
Provided by: michaelv153
Category:
Tags: cookies

less

Transcript and Presenter's Notes

Title: Cookies


1
Cookies
2
About Cookies
  • Remember that you are storing data on the users
    computer
  • Some users may prohibit cookies
  • Cookies are
  • Stored as a text string
  • Text string contains name/value pairs
  • Each name value pair is called a crumb
  • Each name/value pair ends with a semicolon

3
Creating a Cookie
  • The browser (document object) stores one cookie
    per page
  • Its necessary to search the cookie for the
    desired data value
  • The following statement creates a new cookie
  • Document.cookie
  • cookie1Data value

4
Relevant Name/Value Pairs (1)
  • expiresdate
  • Sets the expiration date of the cookie
  • Standard is to used GMT format for the date
  • If the expiration date is not set, then the
    cookie is deleted when the browser is closed
  • If the expiration date is in the past, the cookie
    is deleted
  • Future expiration dates cause the cookie to be
    deleted in that tag

5
Relevant Name/Value Pairs (2)
  • pathpath
  • Sets the cookies path
  • Use to share information with other pages from
    the same domain
  • Note that custom name/value pairs can be created
  • One cookie string can store up to 20 name/value
    pairs

6
Creating a Cookie (Example)
  • function setCookie()
  • var cookieDate new Date() cookieDate.setTime(co
    okieDate.getTime()
  • 24 60 60 1000)
  • document.cookie "name1"
  • document.FormCookie.CookieContent.value
    "expires" cookieDate.toUTCString()
    alert("cookie created "
  • cookieDate.toUTCString()
  • document.FormCookie.CookieContent.value)

7
Reading Cookies
  • The process is less than sophisticated
  • Need to use string functions to parse out the
    name/value pairs
  • indexOf function gets the character position of a
    string
  • length gets string length
  • Use substring to extract the desired value

8
Reading a Cookie (Example part 1)
  • First, see if the cookie exists by searching for
    the name
  • var cookieData new String(document.cookie)
  • var cookieHeader "name1"
  • var cookieStart cookieData.indexOf(cookieHeader)
    cookieHeader.length
  • cookieEnd cookieData.indexOf("",cookieStart)

9
Reading a Cookie (Example part 2)
  • Check cookie exists and display it
  • if (cookieData.indexOf(cookieHeader) ! -1)
  • name cookieData.substring(
  • cookieStart, cookieEnd)
  • alert("Cookie lt" name "gt ")
  • else
  • alert("Cannot find cookie")

10
Deleting a Cookie
  • The process is simple. Just reset the expiration
    time to a date in the past
Write a Comment
User Comments (0)
About PowerShow.com