1

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)

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?

Zeeshan
  • 2,884
  • 3
  • 28
  • 47
  • so what's the purpose of refreshing these cookies in all the tabs? you can use SignalR plugin. – Mox Shah May 29 '14 at 12:42
  • @DotNetIsMyPower i did not mention to 'refresh' cookie, but to share cookie among different browser tabs. Does SignalR helps in my case? – Zeeshan May 29 '14 at 12:52
  • I'm not sure I understand the need to create a cookie to be shared in the first place. If you set a value to an object in one tab it should be available in all tabs, right? You can at least force the issue from tab to tab. – Dean.DePue May 29 '14 at 13:09
  • @Dean.DePue yea, right. It should be immedietly available in other tabs, without need to refresh. This: http://stackoverflow.com/questions/23926873/client-side-script-variables-scope-for-multiple-opened-tabs-of-browser-issue actually is the problem that i'm trying to solve. i decided cookies – Zeeshan May 30 '14 at 04:50

2 Answers2

1

Polling would be checking into a server or other backend service to see if there are new cookies.

So tab 1 creates a cookie and you notify the server of a new cookie. When tab 2 "polls" the server i.e. through a regular ajax request, you can set the cookie on tab 2.

This could also be done with webstorage I believe as 2 tabs of the same page share the same webstorage so you could "poll" the webstorage without needing ajax and also not needing to send anything over the wire. This is quicker, but then you would have to handle setting the cookie manually in the 2nd or "listening" tab. Alternatively if you used webstorage for all storage then you could cut cookies out totally if you don't need them to identify yourself to an off-site/off-client/online service/server.

http://www.w3schools.com/html/html5_webstorage.asp

Can an AJAX response set a cookie?

Community
  • 1
  • 1
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
  • Thanks Rob. Can you please tell me, how can i poll server, when new cookie is set? I tried to look, but nothing helped. (i'm using MVc 4, and cookies is being set and get in Jquery script tag). – Zeeshan May 30 '14 at 05:16
  • Try this jsfiddle in 2 tabs, it should work. At least it does for me in Chrome. http://jsfiddle.net/robschmuecker/8vXKC/ – Rob Schmuecker Jun 02 '14 at 07:18
1

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.

Example 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)
Zeeshan
  • 2,884
  • 3
  • 28
  • 47