Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 43 44 45 46 47 48 < 49 > 50 51 52 53 54 55 .. 118 >> Next

((expires == null) ? ”” : (”; expires=" + expires.toGMTString()))
It thinks to itself “If the value for expires is null, add "" to the document.cookie property. Otherwise, add the string
expires=someGMTFormattedDate to the document.cookie property.”
You can find out more about the conditional ?: operator in Chapter 3.
Accessing a cookie
You can set attributes for a cookie by using JavaScript (specifically, the expires, path, domain, and secure attributes, as I describe in the section “Setting a cookie”), but you can’t access those attributes by using JavaScript. In contrast, you can access a cookie’s value.
This seemingly odd state of affairs — being able to set attributes that you tjyyjyS, can’t retrieve — actually makes sense when you think about it. All these
^ ra yitf ) attributes are security-related, and preventing them from being altered helps
maintain cookies’ integrity and safety. After you give out your cookies, only the Web browser is privy to cookie attributes.
To access a cookie’s value, you query the cookie property associated with the document object. (You see how to set the cookie property in “Setting a cookie,” earlier in this chapter.)
134 Part II: Creating Dynamic Web Pages____________________________________
Check out the following JavaScript code snippet:
var endstr = document.cookie.indexOf(";", offset);
return unescape(document.cookie.substring(offset, endstr));
This code contains two statements:
The first statement uses the indexOf() method to identify the portion
of the myCookie = userName; string between the = and the ; (in other words, to identify the stored value of the userName string).
The second statement unescapes the stored value of the userName string. (Unescaping is computerese for decoding any special characters encoded when the cookie was set.)
You can find a working copy of this code snippet in Listing 6-1, later in this chapter.
Displaying content based on cookie contents: The repeat-visitor script
You can create a script that registers a user by saving the user’s name to the user’s hard drive by using a cookie. On subsequent visits to the site, the script accesses the cookie from the user’s hard drive, recognizes the user’s name, and uses the information to display a custom greeting. Figure 6-5 shows stage one of the repeat-visitor script where users must first register their names.
In many real-life applications, you want to create and access cookies by using a server-side technology, such as a CGI script. Because CGI scripting is beyond the scope of this book, in this chapter I show you how to create and access cookies with JavaScript instead. (The syntax between CGI scripting languages and JavaScript differs, but the basic ways that you interact with cookies are the same.)
After users register their names, as shown in Figure 6-5, they never see the registration form again. Users can close their browsers, turn off their machines, and go away on vacation for a week. When they return and attempt to access the registration page again, the script recognizes that they’ve already registered and loads the For Registered Users Only page with a customized greeting (see Figure 6-6).
Chapter 6: That's How the Cookie Crumbles 135
Figure 6-5:
Registering user input with cookies.
Figure 6-6:
Escorting your registered guest to a reserved page.
136 Part II: Creating Dynamic Web Pages
xAtCO
I implemented the repeat-visitor script in two parts based on the two actions in Figure 6-5 and Figure 6-6:
Cookie Example I (For Unregistered Users page): This script registers a user’s name, stores a cookie on that user’s machine, and loads the For Registered Users Only page.
Cookie Example II (For Registered Users Only page): This script accesses the cookie and displays a custom greeting.
When you create a cookie, you specify an expiration date. After the specified expiration date, the cookie can no longer be accessed. An expiration of the null value marks a cookie as transient. (Transient cookies stay around in memory only as long as the user’s browser session lasts; they aren’t saved to the user’s hard drive.) In the example in Listing 6-1, you see an expiration date of one year from the time the cookie is created.
The Cookie Example I and Cookie Example II scripts are shown in Listings 6-1 and 6-2, respectively. You can find them in the l ist0601.htm and list0602.htm files on the companion CD-ROM.
Listing 6-1: Cookie Example I: The Registration Form
<HTML>
<HEAD><TITLE>Cookie Example I: The Registration Page (From JavaScript For Dummies, 4th Edition)</TITLE>
<SCRIPT LANGUAGE=''JavaScript'' TYPE=”text/javascript”>
<!-- Begin hiding
function getCookieVal (offset) {
// This function returns the portion of the // "myCookie=userName” string // between the = and the ;
var endstr = document.cookie.indexOf (”;”, offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
Previous << 1 .. 43 44 45 46 47 48 < 49 > 50 51 52 53 54 55 .. 118 >> Next