Cookie 和多个浏览器窗口/选项卡
我正在尝试在浏览器窗口/选项卡之间共享一些信息,并且由于浏览器支持问题,我对 HTML5 本地存储持怀疑态度。一些谷歌搜索让我相信 cookie 可以用于此目的,所以我抓住了 jquery.cookie 并设置了一个简单的测试。
加载时写入 cookie 的两个页面:
$.cookie("testValue", new Date().getTime());
以及一个在每个页面上的警报中显示 cookie 值的按钮:
alert($.cookie("testValue"));
测试时,我在每个页面上看到不同的值,这会让我相信这不起作用,但我保留在这里和其他地方看到人们似乎推荐它的帖子,所以我想知道我是否只是做错了什么?
I'm trying to share some information between browser windows/tabs, and I'm leery of HTML5 local storage because of browser support issues. Some googling lead me to believe cookies can be used for this, so I grabbed jquery.cookie and set up a simple test.
Two pages which write a cookie when loaded:
$.cookie("testValue", new Date().getTime());
and a button that displays the cookie value in an alert on each:
alert($.cookie("testValue"));
When testing, I see different values on each page, which would lead me to believe this just doesn't work, but I keep running across posts here and elsewhere where people seem to be recommending it, so I'm wondering if I'm just doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Cookie 仅在由 JS 设置或页面加载时在 JavaScript 环境中更新。
localStorage就是为此而设计的。 IE8 可以很好地支持它,只有真正老的 IE7 及以下版本不支持。尽管 IE9 仅适用于 Windows-Vista 和 7,但 IE8 在 XP 上运行良好,因此您可以毫无困难地要求仍在使用过时浏览器的用户进行更新。
(我不能说太多,但 IE 确实需要像所有其他浏览器一样的自动更新程序......)
Cookies are only updated in the JavaScript environment when set by JS, or when the page loads.
localStorage was designed for this. IE8 supports it just fine, it's only the really old IE7 and below that don't. Although IE9 is Windows-Vista-and-7-only, IE8 is fine on XP so you should have no troubles asking your users who are still on archaic browsers to update.
(I can't say this enough, but IE really need an auto-updater like all other browsers...)
DateTime()
未定义。请改用Date()
。$.cookie("testValue", new Date().getTime());
无论如何,它对我有用。这是一个简单的 html 页面:
使用 FireFox 10.0 在 localhost 中尝试。
DateTime()
is not defined. UseDate()
instead.$.cookie("testValue", new Date().getTime());
Anyway, it works for me. Here's a simple html page:
Tried in localhost using FireFox 10.0.
第二次尝试 =P
尝试这样的操作:
refresher.html:
您仍然可以使用 jQuery
$.cookie
来设置 cookie,但要获取 现在您使用cookiedata.cookiename
。将
5000
调整为响应能力和重新加载数据之间的最佳折衷值。Attempt number two =P
Try something like this:
refresher.html:
You can still use jQuery
$.cookie
to set a cookie, but to get them you now usecookiedata.cookiename
.Adjust the
5000
to whatever seems a good compromise between responsiveness and reloading data.