Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

cookies - Javascript navigator.cookieEnabled Browser Compatibility

How well supported is navigator.cookieEnabled? Can I safely rely on it for all browsers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know it's present in at least IE 6 and later, Firefox 1 and later, and Dottoro reports that it is supported by all major browsers. However, it is not part of any DOM specification and therefore is not guaranteed to be available in or properly implemented by all browsers (for instance, mobile browsers with limited DOM implementations).

As some have discovered, IE returns true for navigator.cookieEnabled even if cookies are blocked for the current site. This means that you cannot currently rely on the property at all and you should avoid it completely.

For a complete cross browser cookie support check, you might want to go with something like this:

var cookies = ("cookie" in document && (document.cookie.length > 0 ||
        (document.cookie = "test").indexOf.call(document.cookie, "test") > -1));

Demo: http://codetester.org/31011785

This will return false in browsers that have cookies disabled or don't support the DOM level 2 property document.cookie, which is about as far as you can go in JS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...