View Single Post
Old 06-18-2011, 10:12 PM   PM User | #12
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
Quote:
Originally Posted by EGS View Post
Please help.
I don't know how to set a cookie with my particular code. Can someone help me & provide it in my code and paste it here? I have no idea what to do.
Here's a set of functions that will save or retrieve data from a cookie or from local storage:

Code for cookies:
Code:
/***
 * write a cookie: name, value, lifetime in days
 **/
var writeCookie = function (name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 86400000));
        var expires = '; expires=' + date.toGMTString();
    } else {
        expires = '';
    }
    var content = name + '=' + value + expires;
    document.cookie = content;
    return content;
}

/***
 * read a cookie: name. returns value or false if not found
 **/
var readCookie = function (name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return false;
}
Example for a cookie:
(1) var name = 'Joe Smith';
(2) writeCookie('username', name, 30); (a cookie named 'username' with a value of 'Joe Smith' is saved on the client's machine and it stays there for 30 days.
(3) .... sometime later.....
(4) var name = readCookie('username');
(5) alert(name); (displays 'Joe Smith' retrieved from the client's machine).


Code for local storage:

Code:
/***
 * read data from local storage, return false if not there
 ***/
var getLocal = function (name) {
    return localStorage.getItem(name) || false;
}

/***
 * save data to local storage, return false if failed
 ***/
var setLocal = function (name, value) {
    localStorage.setItem(name, value);
    if (getLocal(name) !== value) {
        return false;
    }
    return value;
}

/***
 * clear one entry from local storage
 ***/
var clrLocal = function (name) {
    return localStorage.removeItem(name);
}

/***
 * completely wipe all local storage clean
 ***/
var nukeLocal = function () {
    var n = localStorage.length;
    if (n) {
        while (n--) {
            localStorage.removeItem(localStorage[n]);
        }
    }
}
Note that you cannot store OBJECTS in a cookie or in local storage. You must first use JSON.stringifier (to convert an object into a storable form) and JSON.parse (to convert a cookie or local storage string back into an object).

Hope this helps you.......

-- Roger
__________________
"Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
Krupski is offline   Reply With Quote