spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

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

If you plan on using a lot of Cookies on your site, it is preferable to store them in an array, as opposed to storing them using lots of different names. All you need it two basic functions to set and get Cookie arrays (along with the set/get Cookie code). First, to create a Cookie array, use this function:
    function setCookieArray(name){
    this.length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < this.length; i++) {
        data = setCookieArray.arguments[i + 1]
        setCookie (name + i, data, expdate);
        }        
    }
It creates an array of Cookies in the Cookie file, not in memory. There is no way to actually store an array in the Cookie file, but this tricks the Cookies into thinking they are arrays. To create a Cookie array using this function, specifiy the expiration date, name of the Cookie array, and the data to go into the Cookie:
    var expdate = new Date();
    expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

    var testArray = new setCookieArray(
    "_jtotw", <-- name of the Cookie array
    "my",     <-- data for first element of Cookie array
    "data",   <-- data for second element, etc.
    "is",
    "very",
    "important");
The above Cookie array was assigned the name "_jtotw" and was given (as data) 5 words. To get data from a Cookie array, use this function which scans through the Cookie file looking for the specified array and putting it in memory:
    function getCookieArray(name){
    var i = 0;
        while (getCookie(name + i) != null) {
        this[i + 1] = getCookie(name + i);
        i++; this.length = i; 
        }
    }
To access the data that is stored in the "_jtotw" Cookie array, simply make one call to getCookieArray():
    var testArray = new getCookieArray("_jtotw");
After loading the "_jtotw" Cookie array using getCookieArray(), treat the array like you would any other array (but starting at 1, not 0). To print out the first element, or piece of data, in the array, use this:
    document.write (testArray[1]);
The above code would display "my". Grabbing testArray[2] would display "data", etc.

Source


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark