我使用jQuery Cookie插件(downloaddemosource code with comments)来设置和读取Cookie.我正在开发local machine的页面.

The following code will successfully set a cookie in FireFox 3, IE 7, and Safari (PC). But if the browser is Google Chrome AND the page is a local file, it does not work.

$.cookie("nameofcookie", cookievalue, {path: "/", expires: 30});

What I know:

  • The plugin's demo works with Chrome.
  • 如果我把我的代码放在网络服务器上(地址以http:///开头),它可以与Chrome一起工作.

所以cookies 只会失败for Google Chrome on local files次.

Possible causes:

  • Google Chrome doesn't accept cookies from web pages on the hard drive (paths like file:///C:/websites/foo.html)
  • Something in the plugin implentation causes Chrome to reject such cookies

有人能证实这一点并找出根本原因吗?

推荐答案

Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) unless you start it with the --enable-file-cookies flag. You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535.

*如果直接使用本地IP地址(127.0.0.1),Chrome does支持cookies.因此,在localhost的情况下,这可能是一个更简单的解决方法.

Jquery相关问答推荐

如何限制select2中的字符?

即使我使用 [FromBody] C# 从视图(Ajax)发送请求时在控制器中收到 NULL 对象

jquery克隆div并将其附加在特定div之后

$.each() 与 for() 循环 - 和性能

在 jQuery UI Datepicker 中禁用future 日期

将变量传递给jQuery AJAX成功回调中的函数

仅在 _some 字段上通过 Enter 键禁用表单提交

jQuery: Select 属性大于值的所有元素

数据表警告(表 id = 'example'):无法重新初始化数据表

ASP.Net 母版页和文件路径问题

如何使用 jQuery Select 单个子元素?

Twitter Bootstrap alert 可以淡入淡出吗?

ajax调用后jQuery点击功能不起作用?

jQuery $.cookie 不是一个函数

我可以限制 JavaScript 中数组的长度吗?

从父 node 中删除所有子 node ?

如果不是 jQuery,Javascript 中的美元符号是什么

图片转Base64

jQuery:如何找到父母的特定子元素?

在 jquery 中使用 AJAX Post 从强类型 MVC3 视图传递模型的正确方法