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 .. 45 46 47 48 49 50 < 51 > 52 53 54 55 56 57 .. 118 >> Next

3. When a user clicks the Register button, the interpreter begins executing the register() function, which in turn invokes the setCookie() method to store a cookie on the user’s machine. The cookie contains the user’s name and an expiration date.
4. After the register() function stores the cookie, the register() function loads the For Registered Users Only page.
Check out Listing 6-2 to see an example of how to access a cookie to create and display a custom greeting.
Listing 6-2: Cookie Example II: Displaying the Custom Greeting
<HTML>
<HEAD><TITLE>Cookie Example II: The Custom Greeting (From JavaScript For Dummies, 4th Edition)</TITLE>
(SCRIPT LANGUAGE=”JavaScript”>
<!-- Begin hiding
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (”;”, offset); if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (name) {
var arg = name + "=";
var argLength = arg.length;
var cookieLength = document.cookie.length;
Chapter 6: That's How the Cookie Crumbles 141
var i = 0; while (i < cookieLength) { var j = i + argLength; if (document.cookie.substring(i, j) == arg) { return getCookieVal(j) i
if (i == 0) { break }
} return null; }
////////////////////////////////////////////////////////// // This code checks to see whether a cookie named // 'myCookie' exists on the user's machine. // // If it does, the user has already logged in with a valid // userlD and password, so display the site; otherwise, // display an error. //////////////////////////////////////////////////////////
// If the "myCookie” cookie exists . . .
// #1 (down to document.write(documentText)
var nameOfVisitor = getCookie('myCookie')
insert // #2 (down to closing brace associated with if statement) if(nameOfVisitor != null) {
var documentText = "<BODY><H1>Cookie Example II</H1>Welcome to registered portion of my site, " documentText += nameOfVisitor documentText += "!</BODY>” } the
insert // #3 (down to closing brace associated with else statement) else { var documentText = "<BODY><H1>Cookie Example II</H1>Sorry! Only users can access this page.</BODY>” } registered
document.write(documentText)
// End hiding --> </SCRIPT> </HEAD> </HTML>
142 Part II: Creating Dynamic Web Pages
You can't expire me . . . I quit!
You can't delete a cookie directly by using JavaScript for the simple reason that only browsers can actually write to the visitor's hard drive. (It's this security measure that prevents cookies from being able to wreak havoc on users' hard drives.)
What you can do in JavaScript is to alter a cookie's expiration date to a date far in the past. Doing so causes the Web browser to delete the newly expired cookie automatically.
function deleteCookie () { var expired = new Date();
// You can't delete a cookie file directly from the user's
// machine using JavaScript, so mark it as expired by // setting the expiration date to a date in the past.
// First, set the exp variable to a date in the past . . .
expired.setTime (expired.getTime() - 1000000000);
// Then, get the cookie
var cookieValue = getCookie ('myCookie');
// Finally, set the cookie's expiration date to the long-past date. document.cookie = 'myCookie' + ”=” + cookieValue + ”;
expires=” + expired.toGMTString();
}
In Listing 6-2, here’s what’s going on:
1. The JavaScript interpreter looks for a cookie named myCookie on the user’s machine.
2. If a cookie named myCookie exists, the JavaScript interpreter constructs and displays a custom greeting with the registered user’s name.
3. If no such cookie exists, the JavaScript interpreter constructs an error message.
Chapter 7
Working with Browser Windows and Frames
In This Chapter
^ Using JavaScript to open and close pop-up windows ^ Positioning content inside windows ^ Sharing information between frames with JavaScript
B
rowser windows and frames are the lenses through which your users view your Web page content.
As a Web page designer, you can choose to create Web pages that open in a single browser window, which is the standard approach. But with JavaScript, you can do much more. You can display content in separate windows and close those windows automatically. You can even display multiple HTML documents inside a single browser window by using frames, and then share information between those frames by using JavaScript.
By using JavaScript, you can create all kinds of sophisticated window and frame effects. This chapter shows you how.
Whether to include HTML frames in your Web site is a personal design decision. Some folks love frames because they not only allow you to create effective navigation structures, they also allow you to provide hyperlinks to other sites while discouraging users from surfing away to those hyperlinked sites and abandoning your site. The downside? Frames can be complicated to implement, and some people dislike the fact that they hide URL information. (Basically, the URL for a link that’s open in a frame doesn’t appear in the Address bar of the browser.) To see the URL for a link opened in a frame, for example, you can’t just click the link; you must right-click and select Properties (Internet Explorer) or This FrameOView Page Info (Navigator).
Previous << 1 .. 45 46 47 48 49 50 < 51 > 52 53 54 55 56 57 .. 118 >> Next