简体   繁体   English

是!document.cookie可靠吗?

[英]Is !document.cookie reliable?

I'm wondering if using the following Javascript code is reliable: 我想知道使用以下Javascript代码是否可靠:

if (!document.cookie) {
    alert('Cookies are disabled.');
}

I've tested this in IE, Firefox and Chrome and it seems that when you disabled cookies, the document.cookie object becomes unavailable. 我已经在IE,Firefox和Chrome中对此进行了测试,看来当您禁用cookie时,document.cookie对象变得不可用。 Does anyone have any experience with this method working/not working? 有没有人有这种方法工作/不工作的经验?

Many Thanks 非常感谢
Stephen 斯蒂芬

Additional 额外

I'm well aware that this method requires JavaScript to be enabled on the client. 我很清楚这种方法需要在客户端上启用JavaScript。 I'm also aware of other server-side/JavaScript solutions. 我也知道其他服务器端/ JavaScript解决方案。 Please can the discussion remain on topic. 请讨论继续讨论主题。

In XHTML documents, there is no document.cookie at all (up to Firefox 2 or forever on if you send the document as application/xml ). 在XHTML文档中,根本没有document.cookie (如果您将文档作为application/xml发送,则直到Firefox 2或永远不会)。 I had to learn painfully, that it can be set on document , however: 我必须痛苦地学习,它可以在document上设置,但是:

document.cookie = "foo";

This is valid JS, and the browser shrugs its shoulders and sets the property cookie of the variable document . 这是有效的JS,浏览器耸耸肩并设置变量document的属性cookie But the special magic to transform this in an HTTP header doesn't get called. 但是在HTTP标头中转换它的特殊魔法不会被调用。

To put it in a nutshell: No, you can't be sure, that the absence of document.cookie is always identical with disabled cookies, and vice versa. 简而言之:不,你不能确定, document.cookie的缺席总是与禁用的cookie相同,反之亦然。

The only reliable way to me in this scenario (check if cookies are disabled , you don't mind about the javascript issues, and need a client-side solution) is to use a set function for a test cookie, then a get function to read it back. 在这种情况下我唯一可靠的方法(检查cookie是否被禁用 ,你不介意javascript问题,需要客户端解决方案)是使用set函数测试cookie,然后获取get函数读回来。 If the test cookie can't be read back, cookies are off. 如果无法回读测试cookie,则会关闭cookie。

You can write your own implementation of it reading a great resource from quirksmode , use a jQuery plugin or an out-of-box solution . 您可以编写自己的实现,从quirksmode读取一个很好的资源,使用jQuery插件开箱即用的解决方案

Try setting a value on the server, and reading it on the client. 尝试在服务器上设置一个值,然后在客户端上读取它。 If cookies are enabled, you should be able to read the same value. 如果启用了cookie,您应该能够读取相同的值。 If not, they are disabled. 如果没有,他们被禁用。 Note that the site might have httpOnly enabled. 请注意,该站点可能已启用httpOnly。

Opera 7.10 will not understand document.cookie, so it is not reliable. Opera 7.10不会理解document.cookie,所以它不可靠。 Try using this one instead: 请尝试使用此代码:

<script type="text/javascript">
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
}

//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever

</script>

It is compatible with most browsers and the ones which will not work with it are not used anymore. 它与大多数浏览器兼容,不再使用的浏览器不再使用。 I have tested it with Internet Explorer 8.0, Firefox 3.6, Google Chrome 4.0, Opera 10.10 both within HTML and XHTML. 我已经在HTML和XHTML中使用Internet Explorer 8.0,Firefox 3.6,Google Chrome 4.0,Opera 10.10进行了测试。 While using HTML version with Internet Explorer 8.0 I had to confirm execution of the script. 在Internet Explorer 8.0中使用HTML版本时,我必须确认脚本的执行。

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

if(typeof navigator.cookieEnabled == 'undefined' && !gotCookie) {
    document.cookie = 'test';
    gotCookie       = (document.cookie.indexOf('test') != -1) ? true : false;
    }

if gotCookie == true , then you've gotCookie :) 如果gotCookie == true ,那你就得到了gotCookie :)

note: when there's no cookie set, document.cookie seems to be unavailable even if cookie is enabled in the browser. 注意:当没有设置cookie时,即使在浏览器中启用了cookie, document.cookie似乎也不可用。 that's why we set it with document.cookie = 'test' , then check it on the next line. 这就是为什么我们用document.cookie = 'test'设置它,然后在下一行检查它。 of course, assuming that js is enabled. 当然,假设启用了js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM