是!document.cookie可靠吗? [英] Is !document.cookie reliable?
问题描述
我想知道使用下列Javascript代码是否可靠:
I'm wondering if using the following Javascript code is reliable:
if (!document.cookie) {
alert('Cookies are disabled.');
}
我在IE,Firefox和Chrome上测试过,您禁用cookie,document.cookie对象变得不可用。有没有人有这种方法的工作/不工作的经验?
I've tested this in IE, Firefox and Chrome and it seems that when you disabled cookies, the document.cookie object becomes unavailable. Does anyone have any experience with this method working/not working?
很多感谢
Stephen
Many Thanks
Stephen
其他
我很清楚,此方法需要在客户端上启用JavaScript。我也知道其他服务器端/ JavaScript解决方案。
I'm well aware that this method requires JavaScript to be enabled on the client. I'm also aware of other server-side/JavaScript solutions. Please can the discussion remain on topic.
推荐答案
在这种情况下,唯一可靠的方法是检查Cookie是否禁用,你不介意的JavaScript问题,需要一个客户端解决方案)是使用一个测试cookie的set函数,然后一个get函数读取它。
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. If the test cookie can't be read back, cookies are off.
您可以编写自己的实现,从 quirksmode ,请使用 jQuery插件或开箱即用的解决方案。
You can write your own implementation of it reading a great resource from quirksmode, use a jQuery plugin or an out-of-box solution.
这篇关于是!document.cookie可靠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!