English 中文(简体)
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.

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签