Cookie 和多个浏览器窗口/选项卡

发布于 2025-01-06 09:58:11 字数 395 浏览 1 评论 0原文

我正在尝试在浏览器窗口/选项卡之间共享一些信息,并且由于浏览器支持问题,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2025-01-13 09:58:11

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...)

圈圈圆圆圈圈 2025-01-13 09:58:11

DateTime() 未定义。请改用 Date()

$.cookie("testValue", new Date().getTime());

无论如何,它对我有用。这是一个简单的 html 页面:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>demo</title>  
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>  
  <script type='text/javascript' src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
  <script type='text/javascript'>
    $(window).load(function(){
        $("#get").click(function(){
            alert($.cookie("testValue"));
        });
        $("#set").click(function(){
            $.cookie("testValue", new Date().getTime());
        });
    });
  </script>
</head>
<body>
  <a id="get" href="#">get cookie</a>
  <a id="set" href="#">set cookie</a>
</body>
</html>

使用 FireFox 10.0 在 localhost 中尝试。

DateTime() is not defined. Use Date() instead.

$.cookie("testValue", new Date().getTime());

Anyway, it works for me. Here's a simple html page:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>demo</title>  
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>  
  <script type='text/javascript' src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
  <script type='text/javascript'>
    $(window).load(function(){
        $("#get").click(function(){
            alert($.cookie("testValue"));
        });
        $("#set").click(function(){
            $.cookie("testValue", new Date().getTime());
        });
    });
  </script>
</head>
<body>
  <a id="get" href="#">get cookie</a>
  <a id="set" href="#">set cookie</a>
</body>
</html>

Tried in localhost using FireFox 10.0.

喵星人汪星人 2025-01-13 09:58:11

第二次尝试 =P

尝试这样的操作:

<iframe id="cookies" src="refresher.html" style="display: none"; />
<script type="text/javascript">
    (function() {
        var ifr = document.getElementById('cookies');
        setInterval(function() {
            var cd = ifr.contentDocument.cookie.split(";");
                l = cd.length, i, k, ret = {};
            for( i=0; i<l; i++) {
                k = cd[i].split("=");
                k[0] = k[0].replace(/^ +| +$/g,'');
                k[1] = k[1].replace(/^ +| +$/g,'');
                ret[k[0]] = k[1];
            }
            window.cookiedata = ret;
        },5000);
    })();
</script>

refresher.html:

<script type="text/javascript">setTimeout(function() {location.reload();},5000);</script>

您仍然可以使用 jQuery $.cookie设置 cookie,但要获取 现在您使用cookiedata.cookiename

5000 调整为响应能力和重新加载数据之间的最佳折衷值。

Attempt number two =P

Try something like this:

<iframe id="cookies" src="refresher.html" style="display: none"; />
<script type="text/javascript">
    (function() {
        var ifr = document.getElementById('cookies');
        setInterval(function() {
            var cd = ifr.contentDocument.cookie.split(";");
                l = cd.length, i, k, ret = {};
            for( i=0; i<l; i++) {
                k = cd[i].split("=");
                k[0] = k[0].replace(/^ +| +$/g,'');
                k[1] = k[1].replace(/^ +| +$/g,'');
                ret[k[0]] = k[1];
            }
            window.cookiedata = ret;
        },5000);
    })();
</script>

refresher.html:

<script type="text/javascript">setTimeout(function() {location.reload();},5000);</script>

You can still use jQuery $.cookie to set a cookie, but to get them you now use cookiedata.cookiename.

Adjust the 5000 to whatever seems a good compromise between responsiveness and reloading data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文