JavaScript Tip of the Week for July 1st, 1996: Part 1: The Cookie Functions | WebReference

JavaScript Tip of the Week for July 1st, 1996: Part 1: The Cookie Functions

JavaScript Tip of the Weekfor July 1st, 1996: Part 1: The Cookie Functions

Kind of neat, isn't it. Now, every time you come to this tip it will remember your name and the number of times you have been here. How is this done? Using the cookie of course. But there are a few functions that you need to be able to write to and read from the cookie. Fortunately for us, someone by the name of Bill Dortch has already made these easy to use functions readily available to the public. For now, you will not need to exactly how these functions work, but you will need to know how to use them. The first function is used to save to a cookie:
function SetCookie (name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");

If you are a little confused with how this function works, don't worry. It is more important that you understand how it is used to save information. For instance, if you want to save someone's first name to a cookie called "user_name", then this is what you would use:
function set_name(form) {
        var expdate = new Date ();
        expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
        SetCookie ("user_name", "Bob", expdate);

First this function gives the cookie an expiration date (how long it will last before it is erased) of one month. All cookies must have an expiration date, and a month is plenty of time. Then the SetCookie() function is run: It creates a cookie called "user_name", sets the value of the cookie to "Bob", and sets the expiration date to 31 days.

Now how would you read the information off the cookie? Use these two other functions do so:
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 alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
return null;
These two functions work hand-in-hand, so it is important that they are always together. Now using these functions to read the information that was saved on cookie "user_name", you would use a simple line of code like this:
This would print out the value of the "user_name" cookie, which was set to "Bob". If you wanted to print out the value of some other cookie, you would just use it's name in place of "user_name". You can give the value of a cookie to a variable, allowing you to manipulate the values and create programs that can save and load information. But keep in mind that you will need to include all three functions: SetCookie(), GetCookie(), and getCookieVal(), to do anything with the cookies.