300 likes | 412 Views
Working with Cookies. Goals. By the end of this lecture you should … Understand the different types of cookies. Understand how JavaScript keeps track of cookies using the document.cookie object. continued …. Goals. By the end of this lecture you should …
E N D
Goals By the end of this lecture you should … • Understand the different types of cookies. • Understand how JavaScript keeps track of cookies using the document.cookie object. continued …
Goals By the end of this lecture you should … • Understand how to create new cookies. • Understand how to set an expiration date for a cookie. • Understand how to delete a cookie. • Understand how to update a cookie’s value.
What is a Cookie? • A cookie is a small text file that JavaScript can use to store customized information about a user. • There are two types of cookies: • Session Cookies (aka Transient Cookies) • Persistent Cookies
Session Cookies • A browser stores session cookies in memory. • Once a browser session ends, browser loses the contents of a session cookie.
Persistent Cookies • Browsers store persistent cookies to a user’s hard drive. • We can use persistent cookies to customize information about a user that we can use when the user returns to a website at a later date.
Cookies as Objects • JavaScript deals with cookies as objects. • Specifically, JavaScript works with cookies using the document.cookie attribute. • We can read information from cookies by examining the document.cookie object.
Examples of Cookie Uses • User preferences • Saving form data • Assisting with conveying information to back-end programs • Tracking online shopping habits
Parts of a Cookie Object • name – An identifier by which we reference a particular cookie. • value – The information we wish to save, in reference to a particular cookie. • expires – A GMT-formatted date specifying the date (in milliseconds) when a cookie will expire.
Parts of a Cookie Object • path – Specifies the path of the web server in which the cookie is valid. By default, set to the path of the page that set the cookie. However, commonly specified to /, the root directory. • domain – Specifies the domain for which the cookie is valid. Set, by default, only to the full domain of a page. You may wish to extend the domain to include other computers in your domain. • secure – Specifies that a web browser needs a secure HTTP connection to access a cookie.
Cookie Limitations • A given domain may only set 20 cookies per machine. • A single browser may only store 300 cookies. • Browsers limit a single cookie to 4KB.
Setting a Cookie – General Form window.document.cookie = “cookieName = cookieValue[; expires = expireDate][; path = pathName][; domain = domainName][; secure]”;
Escape Sequences • When we set cookie values, we must first convert the string values that set a cookie so that the string doesn’t contain white space, commas or semi-colons. • We can use JavaScript’s intrinsic escape() function to convert white space and punctuation with escape sequences. • Conversely, we can use unescape() to view text encoded with escape().
Using the Cookie Library • A great, open-source, tool is Bill Dortch’s Cookie Library, which contains a plethora of useful functions for dealing with cookies. • To use the functions, include the syntax on the next slide in your scripts …
Including the Cookie Library <script language=“JavaScript 2.1” type = “text/javascript” src = “cookieLibrary.js”> </script>
Calculation Expiration Dates • JavaScript stores dates as the number of milliseconds since 01/01/1970. • To calculate a new date, we would use a formula derived from milliseconds. • For example, 1 year in milliseconds:year = (365 * 24 * 60 * 60 * 1000)
Calculating Expiration Dates var dNow = new Date(); var intTime = new Number(); intTime = dNow.getTime() + (365 * 24 * 60 * 60 * 1000); var dExpire = new Date(intTime);
Calling SetCookie() • We can call the SetCookie() function from the Cookie Library using the following syntax:SetCookie(name, value [, expires] [, path] [, domain] [, secure]);
Calling GetCookie() • We can call the GetCookie() function from the Cookie Library using the following syntax:GetCookie(cookieName);
Calling DeleteCookie() • We can call the DeleteCookie() function from the Cookie Library using the following syntax:DeleteCookie(name, [, path] [, domain]);
Modifying a Cookie • We can call the SetCookie() function from the Cookie Library to modify a cookie, essentially overwriting the previous cookie.
Summary • Session cookies are available to a browser so long as a browser session is active; once the browser closes, it loses all session cookies. • Browsers save persistent cookies to a user’s hard drive; the values of persistent cookies are available across multiple browser sessions. continued …
Summary • Cookies consist of a name, value, expiration date, path, domain and secure flag. • Browsers store cookie modification dates in milliseconds, from 01/01/1970. continued …
Summary • We can use the SetCookie() function from the open-source Cookie Library to set or modify a cookie. • We can use the GetCookie() function from the open-source Cookie Library to retrieve an existing cookie’s value. • We can use the DeleteCookie() function from the open-source Cookie Library to delete a cookie.
Resources • Spain-McDuffie, Tina. JavaScript Concepts & Techniques: Programming Interactive Web Sites. Wilsonville, OR: Franklin, Beedle & Associates, 2003.