开发者

Can I modify or add cookies from JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_运维问答

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 years ago.

Improve this question

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


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.


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


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

For more information about cookies, check this


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜