2

How do I get window.name to 'stick' between refreshes?

I'm trying to use window.name to distinguish between multiple browser windows, so each one can show different kinds of data but on the same URL.

But window.name won't stick.

Here's my test code in the middle of the page...

document.write( '<br> before set is: ' + window.name ); 
window.name = "blah"; 
document.write( '<br> after set is: ' + window.name ); 

And here's the page output, which shows that it's not sticky...

before set is: showframe
after set is: blah 
Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • there are only 2 lines of output which means that there must have been a javascript exception on the line above the last `document.write` – jbabey Jun 26 '12 at 17:41
  • So what's your question? Why are you concerned of the fact that you can change the name of the window? – MaxArt Jun 26 '12 at 17:51
  • Sorry -- I accidentally left that 3rd line in. Page edited. THE QUESTION: How do I get window.name to 'stick' between refreshes? – Doug Null Jun 26 '12 at 20:08
  • do you allow opening various window from same .html / .php page at the same time? – Sebas Jun 26 '12 at 20:12
  • Yes. User can open two browser windows from same page, which allows viewing polled embedded data from multiple sources. – Doug Null Jul 03 '12 at 19:46
  • Oops party is finished... but why dont you use `timestamp` and a long random number to differentiate it.... – Arjun Vachhani Sep 25 '13 at 11:29

1 Answers1

1

window.name does stick. Besides my own observations, this is backed by 3 sources:

  • Your own code. Where is "showframe" coming from? Answer: This is the window.name that was set previously by some other page in that window. You then overwrite it and get this new value. This is the expected behavior.

  • https://developer.mozilla.org/en-US/docs/Web/API/Window/name says

    Modern browsers will reset Window.name to an empty string if a tab loads a page from a different domain

    ...which (kinda) means that it sticks if a tab loads a page from the same domain ;-)

  • https://stackoverflow.com/a/35596134/1668200 recommends window.name instead of sessionStorage

Final tip: Never start by reading the existing window.name - this might be the value from some previous page.

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99