如何测试浏览器是否使用JavaScript接受Cookie? [英] How to test whether a browser accepts cookies with JavaScript?

查看:119
本文介绍了如何测试浏览器是否使用JavaScript接受Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript进行测试,无论浏览器是否支持Cookie。以下代码适用于Internet Explorer 8和Firefox 3.6,但不适用于Google Chrome 5。

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会显示「启用Cookie!不管我的cookie设置。

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.

想知道如何使用Chrome中的JavaScript测试Cookie可用性吗?

Any idea how to test cookie availability with JavaScript in Chrome?

推荐答案

唯一可靠的方法来判断是否启用Cookie是设置一个,并检查它在下一个请求 。需要服务器端代码。

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

这篇关于如何测试浏览器是否使用JavaScript接受Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆