I have an application that uses window.name as a fallback storage to sessionStorage.
Example (simplified for brevity):
function setData(data){
window.name = JSON.stringify(data);
}
function getData(data){
return JSON.parse(window.name);
}
This all works fine and dandy in just about all browsers. However, I have recently discovered that this DOES NOT work in IE7 and below when the window is launched with a target.
<a href="winning.html" target="bob">winning.html</a>
If I launch the winning.html file from the above A, using IE7, getData will always return “bob”, regardless of what we try to set through setData.
If I modify the launch link like so (remove the target), it will set and remember data correctly.
<a href="winning.html">winning.html</a>
The problem is that I do not always have control of that originating link. Is there any way around this?
As is often the case, putting this problem down for a couple of days, and then coming back to it fresh has revealed the answer.
I tested the bug using this script (from this Sitepoint article) for the window.name session storage.
As you might guess, this script is not affected by the bug in my original post. Why? What makes this one work, and my simple example above not work? This bit right here is the key:
For some reason, you MUST save the data back into the
window.namecontainer within the window’sunloadevent.Beware of using only the onunload method for this, as mobile safari will not remember the data. To get mobile safari to persist the data in
window.name, you must save it manually before onunload. Essentially, for maximum compatibility, you must do both: save in onunload; save manually whenever new data is set.