15

Exactly what are the restrictions for handling browser cookies from javascript? Can I check if cookies are enabled for example?

user207414
  • 171
  • 1
  • 1
  • 5

6 Answers6

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 as for testing whether they are enabled. I like jldupont's answer.

jessegavin
  • 74,067
  • 28
  • 136
  • 164
7

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

jldupont
  • 93,734
  • 56
  • 203
  • 318
4

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

For more information about cookies, check this

Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
1

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

Mario Fink
  • 674
  • 4
  • 10
1

Can I check if cookies are enabled for example?

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

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

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

bobince
  • 528,062
  • 107
  • 651
  • 834
0

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

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

JavaScript 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 at TechPatterns.com Javascript Cookie Script Get Cookie, Set Cookie, Delete Cookie Functions work better (#1 in Google results isn't always the best).

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

neophyte
  • 6,540
  • 2
  • 28
  • 43
Bratch
  • 4,103
  • 5
  • 27
  • 32
  • These libraries appear to have another problem though (if the cookie contains '='): http://thinkclay.com/technology/add-edit-delete-cookies-with-javascript#comment-712789590 – despot Dec 27 '12 at 12:47