开发者

Checking for cookies being enabled in IE8 using Javascript

开发者 https://www.devze.com 2022-12-22 09:50 出处:网络
I am attempting to check whether cookies are enabled or not using Javascript, cross-browser. I have got this working fine in Firefox 3 using the following code -

I am attempting to check whether cookies are enabled or not using Javascript, cross-browser.

I have got this working fine in Firefox 3 using the following code -

var cookieEnabled=(navigator.cookieEnabled)? true : false;

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
    document.cookie="testcookie";
    cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
}

And from what I've seen this seems to be the reasonably established method of checking for cookie support being turned on? However, it refuses to work in IE8, when I turn off cookies in that and run this code cookiesEnabled always equals true开发者_运维知识库.

Any ideas?

Thanks.


Ah found the problem. I was testing my software by navigating to http://localhost...

Of course IE treats this as the Local Intranet Zone and doesn't apply my settings for cookies, so allowing them regardless!

If I navigate to my actual IP address via the local loopback it treated it as internet zone and worked fine :)

Doh!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号