Javascript navigator.cookieEnabled Browser Compatibility

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

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

4条回答
  •  清歌不尽
    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.

提交回复
热议问题