在浏览器标签页之间共享Cookie,而不刷新标签页 [英] Sharing Cookie among browser tabs without refreshing tab

查看:1588
本文介绍了在浏览器标签页之间共享Cookie,而不刷新标签页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在浏览器的不同标签(同一应用程序)之间共享 Cookie 的值。我工作,发现其他标签只能使用cookie,由第一选项卡设置,如果第二选项卡刷新。但在我的情况下,我买不起刷新标签(& i不能使用会话)。有没有办法以其他方式共享cookie? (即,一旦在一个选项卡上设置了Cookie,其他选项卡就可以使用它,而无需刷新)

I need to share Cookie value among different tabs of browser (same application). I worked around and found that other tab can only use cookie, set by 1st tab, if 2nd tab is refreshed. But in my case, i can't afford refreshing tab (& i can't use sessions). Is there any way to share cookie other way? (i.e, soon as cookie is set on one tab, the other tab can use it, without need to being refreshed)

SO的另一篇帖子,必须继续轮询Cookie,以便共享。但我不明白, poll 是什么意思。

Another post at SO said, one must continuesly poll cookie, in order to share. but i couldn't understand, what they mean by poll. can this poll thing help in my case?

推荐答案

无论如何,轮询服务器无法帮助我的场景(不同的标签,相同的应用程序)。我使用一些库来解决它,名为 store.js 。它不需要轮询服务器和东西,而是一个cookie只是立即共享在选项卡上,很快,因为它设置在任何打开的选项卡。最重要的是,我喜欢的是,store.js使用 localStorage (如果可用),并返回 userData IE6中的行为和 IE7 。没有闪光灯可减缓您的网页载入速度。没有Cookie可以填充您的网络请求。 store.js依赖于JSON序列化到磁盘。使用非常简单。

Somehow, polling the server could not help in my scenario (different tabs, same app). I solved it using some library, called store.js. It doesn't require polling server and stuff, instead, a cookie is simply shared instantly across the tabs, very soon as it is set on any of the opened tabs. and above all, what i loved about this is that store.js uses localStorage when available, and falls back on the userData behavior in IE6 and IE7. No flash to slow down your page load. No cookies to fatten your network requests. store.js depends on JSON for serialization to disk. It's very simple to use.

示例使用:

store.set('username', 'joe')
document.write('username: ' + store.get('username') + '<br />')
store.remove('username')
store.clear()
store.set('user', { name: 'joe', likes: 'javascript' })
var user = store.get('user')
document.write(user.name + ' likes ' + user.likes)

这篇关于在浏览器标签页之间共享Cookie,而不刷新标签页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆