![]() |
![]() |
|
|
||
(Last week's "How to Use Cookies, Part II" article talks about what cookies are, reasons to use cookies, and how they work. The WillMaster Possibilities archives at http://willmaster.com/possibilities/archives has a link to that article.) Here you will find how to implement JavaScript cookie code on your site. The code will determine you how many times the visitor has loaded the page at your site. You can use that number to
First, retrieve the JavaScript code from the demo page at http://willmaster.com/a/6/pl.pl?art69 Lines 6 through 54 contain the required JavaScript cookie code. There is one variable, "DaysToLive" (on line 17), that you need to modify. The number you provide here is the number of days you want the cookie to remain on the visitor's computer. No other changes are required, just copy and paste the cookie code into your own page -- in the HEAD section just like on the demo page. Lines 61 through 67 contain a function to launch a popup window. It could be any other function you want to run when the visitor has been at your page(s) a specific number of times, so feel free to customize or replace with your own. Lines 85 through 102 are a demonstration of how to use the JavaScript cookie code. These lines, and how to customize them, are explained here. The line
visits = GetCookie('page_name');
is where your page finds out how many times the visitor has been at your page(s). If it's the first visit, the value in the variable "visits" will be the digit 1. If the second, the value will be 2, and so on. However, if your visitor has the browser's cookies turned off, the number will not increment. The parenthesis in the call to the GetCookie() function contains the name of the cookie that keeps count. (The name is between apostrophes.) In the example, the name is: page_name If you use this JavaScript cookie code only on one page, you can leave the cookie name as is, or use anything else you want (only alphanumerics and underscores, no spaces, in the cookie name). However, if you will use the cookie code on multiple pages, here are considerations:
The lines
if(visits == 1) {
document.write('<h2>Welcome!</h2>');
}
if(visits == 2) {
document.write('<h2>Welcome, again!</h2>');
}
if((visits >= 3) && (visits <= 9)) {
document.write('<h2>Welcome, friend!</h2>');
}
if(visits > 9) {
document.write('<h2>Welcome, loyal friend!</h2>');
}
determine the content of a heading on the page. If it is the first visit to the page (or group of pages), the heading is "<h2>Welcome!</h2>". If it is visit number 2, the heading is "<h2>Welcome, again!</h2>". If it is the third through ninth visit, the heading is "<h2>Welcome, friend!</h2>". And for every visit after the ninth visit, the heading is "<h2>Welcome, loyal friend!</h2>". Use the above example to put your own custom content on your page, content depending on how many times your visitor has loaded your page(s). The lines
if(visits == 1) { Popup('firstvisit.html',500,150); }
if(visits == 2) { Popup('secondvisit.html',500,150); }
if(visits == 15){ Popup('loyal.html',600,200); }
determine whether or not a JavaScript function is run, the
determination depending on the number of times your visitor
has loaded your page(s).
If it is the first visit to the page, the function Popup() is run to make a popup window 500 pixels wide and 150 pixels high. In the popup window will be the page found at URL firstvisit.html. If it is the second visit, the function Popup() is run to make a popup window the same size as the first one. This time, the page in the window is secondvisit.html. Then, when it is visit number 15, the function is run again. The window is 600 high and 200 wide; and contains the page at URL loyal.html. You may substitute your own JavaScript functions. That makes your page very customizable. That's all there is to it! Next week's "How to Use Cookies, Part IIII" will have a CGI cookies implementation for you, one that will pre-fill some form fields for your visitor. Watch for it. Copyright 2000 William Bontrager
| ||