[ACCEPTED]-Can I modify or add cookies from JavaScript?-client-side

Accepted answer
Score: 42

Yes! Read this excellent article about using cookies with JavaScript

Here's an excerpted code example.

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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 null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

And 2 as for testing whether they are enabled. I 1 like jldupont's answer.

Score: 7

You write a cookie and try to read back: this 1 way, you'll know if cookies are enabled.

Score: 4

you can use navigator.cookieEnabled but I'm not sure if it's supported 2 by all browsers.

For more information about 1 cookies, check this

Score: 1

Can I check if cookies are enabled for example?

Yes, but 16 not as easily as you think. navigator.cookieEnabled is a very general 15 flag which does not cover exactly under 14 what circumstances you may set a cookie.

For 13 example, it's possible for session cookies 12 to be allowed but persistent cookies blocked. So 11 you're not really going to know whether 10 a cookie-set will succeed unless you go 9 ahead and try it, by setting a dummy document.cookie and 8 then reading document.cookie back to see if it took.

In 7 many browsers a persistent cookie will be 6 downgraded to a session cookie when persistent 5 cookies are disabled. But not IE, which 4 will simply block it. You can try to detect 3 that by setting both a persistent and a 2 session cookie to document.cookie and seeing which if any 1 survives.

Score: 1

There's a great article on quirksmode about 1 cookie manipulation via JavaScript: http://www.quirksmode.org/js/cookies.html

Score: 0

The W3Schools JavaScript Cookies code has a bug in it. In 10 the function setCookie this line:

exdate.setDate(exdate.getDate()+expiredays);

JavaScript 9 Date Object Properties:

getDate() - Returns the day of the month (from 1-31)  
...  
getTime() - Returns the number of milliseconds since midnight Jan 1, 1970  
... 

getDate() plus the number of days is not going to work.  I think it should be something like this:  

expire = expiredays * 1000 * 60 * 60 * 24; // convert to milliseconds  
var exdate = new Date( today.getTime() + (expire) );

The cookie libraries 8 at TechPatterns.com Javascript Cookie Script Get Cookie, Set Cookie, Delete Cookie Functions work better (#1 in 7 Google results isn't always the best).

I 6 tested code from both pages in IE8 and the 5 first one caused my cookie to have an expire 4 date of 1/1/2038 1:00 AM. The code from 3 the second example set my cookie expire 2 date to exactly 1 day from the time I tested 1 it, just as expected.

More Related questions