Javascript navigator.cookieEnabled浏览器兼容性

问题描述:

受支持程度如何 navigator.cookieEnabled

我知道它存在于至少IE 6及更高版本 Firefox 1及更高版本,Dottoro报告说它是所有主流浏览器支持的 。但是,它不是任何DOM规范的一部分,因此不能保证在所有浏览器(例如,具有有限DOM实现的移动浏览器)中可用或正确实现。

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).

正如一些人已经发现,即使当前网站的Cookie被阻止,IE也会为 navigator.cookieEnabled 返回true。这意味着您目前无法依靠该媒体资源,因此您应该完全避免它。

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.

要完成跨浏览器cookie支持检查,您可能需要使用例如:

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));

演示: http: /codetester.org/31011785

这会在浏览器中返回 false 不支持DOM级别2属性 document.cookie ,这是你在JS中可以使用的。

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.