Javascript navigator.cookieEnabled Browser Compatibility

前端 未结 4 676
北荒
北荒 2020-12-08 08:40

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

相关标签:
4条回答
  • 2020-12-08 08:51

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

    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.

    // 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.
    }
    
    0 讨论(0)
  • 2020-12-08 08:53

    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.

    0 讨论(0)
  • 2020-12-08 09:11

    I like this 1 liner function:

    function cookiesEnabled() {
        return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
    }
    
    0 讨论(0)
  • 2020-12-08 09:18

    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.

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

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

    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.

    0 讨论(0)
提交回复
热议问题