Doug Null
Doug Null

Reputation: 8327

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 window will have unique data. ...as opposed to a cookie, which is shared by all the windows.

My users need to open multiple windows, to the same .html page, for viewing dynamic embedded data. Each window will, at the same time, show data dynamically from a different embedded source.

So, my JavaScript needs to be able to distinguish one window from another.

Upvotes: 1

Views: 441

Answers (2)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

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.

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179086

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';

Upvotes: 0

Related Questions