Jquery cookie 监控
在浏览器的整个页面生命周期中监视 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,不可能将
change
(或类似)事件直接绑定到 cookie。相反,我会采用这种方法:创建一个轮询器,每 X 毫秒将 cookie 值与之前已知的值进行比较。
然后使用方式将如下所示:
注意:这还没有被经过测试,这只是我如何解决该问题的快速演示。
编辑:我现在已经对此进行了测试并且它有效。 参见示例:)
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.
Usage would then be something like this:
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 :)
知道吗,这对一些奇怪的事情有帮助。我试图在移动网络应用程序上使用谷歌翻译(http://translate.google.com/translate_tools?hl=en),每次更改语言时,它都会在顶部插入一个栏作为文档的第一个子项。身体 。我现在正在跟踪“googtrans”cookie 并执行此操作
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
有一个用于
cookies
的jQuery
插件。http://plugins.jquery.com/project/Cookie
访问cookie并不难在 html 文档中,它们位于
document.cookie
中。此外,还有用于添加 cookie 的选项参数,例如过期时间。
然而,在回答你的问题时,不幸的是,我实际上并不认为你可以将事件侦听器应用于 cookie。
There is a
jQuery
plugin forcookies
.http://plugins.jquery.com/project/Cookie
It is not difficult to access the cookies in a html document, they live in
document.cookie
.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.