Javascript navigator.cookieEnabled 浏览器兼容性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6125330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:35:08  来源:igfitidea点击:

Javascript navigator.cookieEnabled Browser Compatibility

javascriptcookies

提问by dtbarne

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

支持情况如何navigator.cookieEnabled?我可以安全地将它用于所有浏览器吗?

回答by Andy E

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

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

As some have discovered, IE returns true for navigator.cookieEnabledeven 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.

正如一些人发现的那样,navigator.cookieEnabled即使当前站点的 cookie 被阻止,IE也会返回 true 。这意味着您目前根本不能依赖该属性,您应该完全避免它。

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

要进行完整的跨浏览器 cookie 支持检查,您可能需要使用以下方法:

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

Demo: http://codetester.org/31011785

演示:http: //codetester.org/31011785

This will return falsein 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.

这将false在禁用 cookie 或不支持 DOM 级别 2 属性的浏览器中返回document.cookie,这是您在 JS 中所能达到的程度。

回答by mike nelson

In a quick test just now (using IE9), it appears that navigator.cookieEnabled still returns true when the browser is blocking cookies for that site.

在刚刚的快速测试中(使用 IE9),当浏览器阻止该站点的 cookie 时,navigator.cookieEnabled 似乎仍然返回 true。

In other words, cookies are enabled but not for that particular page you are on.

换句话说,cookies 已启用,但不适用于您所在的特定页面。

Therefore you need to test for whether cookies actually work when you set them. The correct code should be (modified from Andy E's answer):

因此,您需要在设置 cookie 时测试它们是否真的有效。正确的代码应该是(从 Andy E 的回答中修改):

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

There is really no point in checking navigator.cookieEnabled.

检查 navigator.cookieEnabled 真的没有意义。

回答by wayofthefuture

I like this 1 liner function:

我喜欢这个 1 班轮功能:

function cookiesEnabled() {
    return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
}

回答by PHP Guru

navigator.cookieEnabled is not always reliable and does not work at all on old browsers.

navigator.cookieEnabled 并不总是可靠的,并且在旧浏览器上根本不起作用。

This answer will work on all browsers that support JavaScript. Additionally this does not need jQuery and it deletes the test cookie after the test is complete.

此答案适用于所有支持 JavaScript 的浏览器。此外,这不需要 jQuery,它会在测试完成后删除测试 cookie。

// returns 1 or 0 instead of true or false. Returns null if inconclusive.
function cookiesEnabled() {
    var i, j, cookies, found;
    document.cookie = 'testcookiesenabled=1';
    for (i=0; i<2; i++) {
        found = false;
        cookies = document.cookie.split(';');
        j = cookies.length;
        while(j--) {
            while (cookies[j].charAt(0)==' ') {// trim spaces
                cookies[j] = cookies[j].substring(1);
            }
            if (cookies[j].indexOf('testcookiesenabled=')==0) {
                found = true;
                break;
            }
        }
        if (!found) {
            return i;
        }
        // Delete test cookie.
        document.cookie = 'testcookiesenabled=; expires=Thu, 01 Jan 1970 00:00:01 GMT';
    }
    // Results inconclusive.
}