Skip to content Skip to sidebar Skip to footer

Can Javascript Store Data Unique To A Particular Browser Window Instance?

Is there a JavaScript way to store data that is unique to a browser window, so that if the user has multiple browser windows open (all the same browser, say: Firefox), that each w

Solution 1:

The only thing I can think of is setting window.name... I haven't even tried it, but as far as I know, it should survive page navigation like how a cookie does - I'll be back in a sec, going to do a bit of testing...

--

Seemed to work OK, I'm not sure what the maximum length of window.name can be (edit: 2MB cross-browser, much more if you're excluding Opera - http://www.thomasfrank.se/sessionvars.html). The only thing I found was in Chrome, if you visit another domain, the window.name is lost, but in other browsers, the name still there, so it's doable, not 100% certain you should rely on it always working in the future though.

The other way of storing state per window/tab is via the URI, but that can also be made tricky where navigation is concerned. I mean it would require a disciplined use a specific navigation mechanism to make sure it's not accidentally lost, and it would be in plain view of your users too. The upside would be it would be bookmarkable / emailable etc, if that's a design goal.

--

All these methods are going to be unsuitable for personal/sensitive/critical data, but for volatile/transient view-state, I don't see any issue.

Solution 2:

If you attach the data to the various window instances, each window context will be different from the others.

var w = window.open('somepage.html');
window.foo = 'bar';
w.foo = 'baz';

Post a Comment for "Can Javascript Store Data Unique To A Particular Browser Window Instance?"