JavaScript Tip of the Week for November 25, 1996: Everything You Ever Wanted to Know About Cookies | 4 | WebReference

JavaScript Tip of the Week for November 25, 1996: Everything You Ever Wanted to Know About Cookies | 4

JavaScript Tip of the Weekfor November 25, 1996: Everything You Ever Wanted to Know About Cookies

The welcome screen is any easy way to allow new visitors to your site feel welcome and become oriented quickly. Most sites start out by throwing tons of information at new visitors, who may have no idea what the site is about or how it works. Using this technique, you can make "newbies" feel right at home during their first few visits. A welcome page can contain many things: a tour of the site, an explanation of what it's all about and how to use it, or simply a limited, less confusing table of contents.

Of all the things you can do with a Cookie, the welcome screen is the easiest. Assuming you have all the cookie functions in your page, there is very little extra code. The first three lines below set the welcome Cookie's name and expiration date; the latter being far off into the future. The next line, var welcomePage = ... tells the script the URL of the welcome page that you want the new visitors to see. The following lines determine if the user has been to the site before by checking for the cookie and printing out a welcome page if no cookie is found or if the user chose to keep the welcome screen on his/her last visit.
var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
var cookieName = "_welcomeJTotW";        <-- welcome cookie (use a different name!)
var welcomePage = "part03_welcome.html"; <-- welcome page URL
    if (getCookie(cookieName) == null || getCookie(cookieName) == "true") {
    setCookie(cookieName, "true", expdate);
    document.write ('&ltFRAMESET ROWS = "100%, *" FRAMEBORDER = NO BORDER = 0>');
    document.write ('&ltFRAME SCROLLING = AUTO SRC = "' + welcomePage + '"');
    document.write ('MARGINWIDTH = 5 MARGINHEIGHT = 5>');
    document.write ('&lt/FRAMESET>');        
    else if (getCookie(cookieName) == "offtemp") {
    setCookie(cookieName, "true", expdate);
Instead of using the location method (which gets people stuck in the backup-black-hole) this script uses the idiot-proof hidden frame method. No redirection here folks; it simply prints out one frame with the specified "welcome" URL. What happens if the visitor does not have JavaScript enabled? Nothing at all, they just see the normal page.

To allow the visitor to disable the welcome screen so that it does not appear on the next visit, use this small form. It contains a checkbox that toggles the welcome cookie on and off. This is not located in the main page, but in the separate "welcome" page that you've created for the new visitors.
    <INPUT TYPE = "checkbox" onClick = "top.toggleWelcome()">
    Don't show this welcome screen again.
Finally, to allow the new visitor to jump directly to the normal page without permanently disabling the "welcome" page, you can add this to the "welcome" page in addition to the disable checkbox. This function disables the welcome cookie temporarily and reloads the page, bringing the user directly to the normal page. The great thing about this is that it looks and functions like an ordinary link.
    <A HREF = "javascript:top.offTemp()">Enter Main Page</A>
Short, simple, to the point; I've always liked that in a tip.