今天在开发一网页时,利用jquery的cookie插件存取cookie,本地一直调试其他浏览器都没有问题,就谷歌浏览器这小子不听使唤,搞的头都大了,怀疑是浏览器的HACK,后来才知道是谷歌浏览器不支持本地文件的JS文件多Cookie的读取。顺便把找到的资料给大家阅读下。
I am using the jQuery Cookie plugin (download and demo and source code with comments) to set and read a cookie. I'm developing the page on my 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.
If I put my code on a web server (address starting with http://), it works with Chrome.
So the cookie fails only 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 unless you start it with the --enable-file-cookies flag. You can read a discussion about it at Detail