Jquery cookie 监控

发布于 2024-11-05 10:05:41 字数 72 浏览 0 评论 0原文

在浏览器的整个页面生命周期中监视 cookie 并在 cookie 更改时触发事件的最佳 JS 或 JQuery 特定方法是什么?

What's the best JS or JQuery-specific approach to monitor a cookie through-out the page life on the browser and trigger an event, when the cookie is' changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

自控 2024-11-12 10:05:41

据我所知,不可能将 change (或类似)事件直接绑定到 cookie。相反,我会采用这种方法:

创建一个轮询器,每 X 毫秒将 cookie 值与之前已知的值进行比较。

// basic functions from the excellent http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/////////////////////////////////
// ACTUAL FUN STUFF BELOW
/////////////////////////////////

var cookieRegistry = [];

function listenCookieChange(cookieName, callback) {
    setInterval(function() {
        if (cookieRegistry[cookieName]) {
            if (readCookie(cookieName) != cookieRegistry[cookieName]) { 
                // update registry so we dont get triggered again
                cookieRegistry[cookieName] = readCookie(cookieName); 
                return callback();
            } 
        } else {
            cookieRegistry[cookieName] = readCookie(cookieName);
        }
    }, 100);
}

然后使用方式将如下所示:

listenCookieChange('foo', function() {
    alert('cookie foo has changed!');
});

注意:这还没有被经过测试,这只是我如何解决该问题的快速演示。

编辑:我现在已经对此进行了测试并且它有效。 参见示例:)

As far as I know, it is not possible to bind a change (or similar) event to a cookie directly. Instead, I would go for this approach:

Create a poller that compares the cookie value to the previously known value every X milliseconds.

// basic functions from the excellent http://www.quirksmode.org/js/cookies.html
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/////////////////////////////////
// ACTUAL FUN STUFF BELOW
/////////////////////////////////

var cookieRegistry = [];

function listenCookieChange(cookieName, callback) {
    setInterval(function() {
        if (cookieRegistry[cookieName]) {
            if (readCookie(cookieName) != cookieRegistry[cookieName]) { 
                // update registry so we dont get triggered again
                cookieRegistry[cookieName] = readCookie(cookieName); 
                return callback();
            } 
        } else {
            cookieRegistry[cookieName] = readCookie(cookieName);
        }
    }, 100);
}

Usage would then be something like this:

listenCookieChange('foo', function() {
    alert('cookie foo has changed!');
});

Note: this has not been tested and is just a quick demo of how I would approach the problem.

EDIT: I have tested this now and it works. See example :)

寄居人 2024-11-12 10:05:41

知道吗,这对一些奇怪的事情有帮助。我试图在移动网络应用程序上使用谷歌翻译(http://translate.google.com/translate_tools?hl=en),每次更改语言时,它都会在顶部插入一个栏作为文档的第一个子项。身体 。我现在正在跟踪“googtrans”cookie 并执行此操作

listenCookieChange('googtrans', function() {
    $(document.body).css('top','0px');
});

Know what, this helped for something weird. I was trying to use Google Translate (http://translate.google.com/translate_tools?hl=en) on a mobile webapp and everytime the language is changed, it kept inserting a bar at the top as the first child of document.body . I am now tracking the 'googtrans' cookie and doing this

listenCookieChange('googtrans', function() {
    $(document.body).css('top','0px');
});
挥剑断情 2024-11-12 10:05:41

有一个用于 cookiesjQuery 插件。

http://plugins.jquery.com/project/Cookie

访问cookie并不难在 html 文档中,它们位于 document.cookie 中。

<!-- Add cookie -->
$.cookie('cookieName':'cookieValue');

<!-- get cookie -->
$.cookie('cookieName');

<!-- removecookie -->
$.cookie('cookieName', null);

此外,还有用于添加 cookie 的选项参数,例如过期时间。

然而,在回答你的问题时,不幸的是,我实际上并不认为你可以将事件侦听器应用于 cookie。

There is a jQuery plugin for cookies.

http://plugins.jquery.com/project/Cookie

It is not difficult to access the cookies in a html document, they live in document.cookie.

<!-- Add cookie -->
$.cookie('cookieName':'cookieValue');

<!-- get cookie -->
$.cookie('cookieName');

<!-- removecookie -->
$.cookie('cookieName', null);

Additionally there are options arguments for add a cookie, e.g. expiry.

However in reply to your question, I don't actually think you can apply a event listener on to cookies unfortunately.

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