admin管理员组文章数量:1425709
I want to test with JavaScript whether the browser supports cookies or not. The following code works with Internet Explorer 8 and Firefox 3.6 but not with Google Chrome 5.
function areCookiesEnabled() {
document.cookie = "__verify=1";
var supportsCookies = document.cookie.length > 1 &&
document.cookie.indexOf("__verify=1") > -1;
var thePast = new Date(1976, 8, 16);
document.cookie = "__verify=1;expires=" + thePast.toUTCString();
return supportsCookies;
}
if(!areCookiesEnabled())
document.write("<p>Activate cookies!</p>");
else
document.write("<p>cookies ok</p>");
Chrome displays the message "Activate cookies!" regardless of my cookie settings. But if I disallow cookies, Chrome tells me that a cookie could not be set.
Any idea how to test cookie availability with JavaScript in Chrome?
I want to test with JavaScript whether the browser supports cookies or not. The following code works with Internet Explorer 8 and Firefox 3.6 but not with Google Chrome 5.
function areCookiesEnabled() {
document.cookie = "__verify=1";
var supportsCookies = document.cookie.length > 1 &&
document.cookie.indexOf("__verify=1") > -1;
var thePast = new Date(1976, 8, 16);
document.cookie = "__verify=1;expires=" + thePast.toUTCString();
return supportsCookies;
}
if(!areCookiesEnabled())
document.write("<p>Activate cookies!</p>");
else
document.write("<p>cookies ok</p>");
Chrome displays the message "Activate cookies!" regardless of my cookie settings. But if I disallow cookies, Chrome tells me that a cookie could not be set.
Any idea how to test cookie availability with JavaScript in Chrome?
Share Improve this question edited May 23, 2017 at 10:32 CommunityBot 11 silver badge asked Jul 22, 2010 at 15:26 deamondeamon 92.7k116 gold badges330 silver badges460 bronze badges 2- It works fine for me in Chrome 5 (running on Windows 7). – James Commented Jul 22, 2010 at 15:43
- I run Chrome 5 on Windows 7 too, but it doesn't work! – deamon Commented Jul 22, 2010 at 15:48
2 Answers
Reset to default 2The only reliable way to tell if cookies are enabled is to set one, and check for its existence on the next request. Server-side code required.
I'm not sure why the code doesn't work, but you really can simplify your code which will help pin down the problem:
function areCookiesEnabled() {
document.cookie = "__verify=1;expires=" + new Date(1976, 8, 16).toUTCString();
return (document.cookie.length > 1);
}
if(!areCookiesEnabled())
document.write("<p>Activate cookies!</p>");
else
document.write("<p>cookies ok</p>");
Also, here is an excellent overview of Javascript cookies, it might help you pin it down.
本文标签: How to test whether a browser accepts cookies with JavaScriptStack Overflow
版权声明:本文标题:How to test whether a browser accepts cookies with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745415592a2657669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论