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

What Cookies Are and How to Use Them
Storing Cookies In Arrays
Welcome New Visitors with A Special Page



This code is put in the HTML document where you want to utilitize Cookies:
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates, 
 * all rights reserved. In order to receive the right to license this 
 * code for use on your site the original code must be copied from the
 * Web site webreference.com/javascript/. License is granted to user to 
 * reuse this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */

function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) + 
"; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}
// -->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
This code is put in the HTML document where you want to utilitize Cookies stored in Arrays:
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--

/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates, 
 * all rights reserved. In order to receive the right to license this 
 * code for use on your site the original code must be copied from the
 * Web site webreference.com/javascript/. License is granted to user to 
 * reuse this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */

function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) + 
"; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

function setCookieArray(name){
this.length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < this.length; i++) {
        this[i + 1] = setCookieArray.arguments[i + 1]
        setCookie (name + i, this[i + 1], expdate);
        }        
}

function getCookieArray(name){
var i = 0;
        while (getCookie(name + i) != null) {
        this[i + 1] = getCookie(name + i);
        i++; this.length = i; 
        }
}

var expdate = new Date();
expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

var testArray = new setCookieArray("_jtotw", "my", "data", "is", "very", "important");

var testArray = new getCookieArray("_jtotw");
// -->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
document.write ( testArray[1] + " " );
document.write ( testArray[2] + " " );
document.write ( testArray[3] + " " );
document.write ( testArray[4] + " " );
document.write ( testArray[5] + " " );
</SCRIPT>
</BODY>
</HTML>
This code is put in the HTML document where you want to utilitize the Welcome page (most likely one of your main pages, where new visitors would start out):
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates, 
 * all rights reserved. In order to receive the right to license this 
 * code for use on your site the original code must be copied from the
 * Web site webreference.com/javascript/. License is granted to user to 
 * reuse this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */

function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) + 
"; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

function toggleWelcome() {
        if (getCookie(cookieName) == "true") setCookie(cookieName, "false", top.expdate);
        else setCookie(cookieName, "true", top.expdate);
}

function offTemp() {
        if (getCookie(cookieName) == "true") {
        setCookie(cookieName, "offtemp", expdate);
        history.go(0);
        }
}

var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
var cookieName = "_welcomeJTotW";
var welcomePage = "part03_welcome.html";

        if (getCookie(cookieName) == null || getCookie(cookieName) == "true") {
        setCookie(cookieName, "true", expdate);
        document.write ('lt;FRAMESET ROWS = "100%, *" FRAMEBORDER = NO BORDER = 0gt;');
        document.write ('lt;FRAME SCROLLING = AUTO SRC = "' + welcomePage + '"');
        document.write ('MARGINWIDTH = 5 MARGINHEIGHT = 5gt;');
        document.write ('lt;/FRAMESETgt;');        
        }
        else if (getCookie(cookieName) == "offtemp") {
        setCookie(cookieName, "true", expdate);
        }
// -->
</SCRIPT>
</HEAD>
<BODY>

This is the normal Page.

</BODY>
</HTML>
Then put this small form, which turns the welcome screen off, in the "welcome" HTML document:
<FORM>
<INPUT TYPE = "checkbox" onClick = "top.toggleWelcome()">
Don't show this welcome screen again.
</FORM>
You can also include this small link, which brings the user to the normal page by temporarily disabling the welcome cookie:
<A HREF = "javascript:top.offTemp()">Enter Main Page</A>