I never knew two pages can share values like that.
Yeah. Normally, they don't. This is only happening because you used the specific variable name name
, and used it in the global scope where it means the same thing as the window
property of the same name.
window.name
is an oddity dating from the early days of cross-document scripting. As a design it doesn't make any sense today but it's very difficult to get rid of old web behaviours.
When Netscape introduced JavaScript and frames in Navigator 2.0, security wasn't the first priority. The web was a different, less-threatening place, and for business reasons they were more concerned with adding any and all possible features into the browser than designing a coherent and secure platform.
The Same Origin Policy was in its infancy and wasn't regarded as an essential fundamental safeguard, more an undesirable encumberance pending the design something better and more permissive. They had a blacklisting instinct: add features and allow access by default, unless it's proven to be a security problem.
The window.name
property on pop-up windows reflects the name
argument of the window.open
method that was called to open it; on frames, it reflects the name
attribute of the <frame>
(or <iframe>
) element that included it. It was expected that scripts would want to access and navigate related windows (and even sub-windows, eg a frame inside a frame) by name
even across different domains and even when the original document had been navigated.(*)
Consequently a number of properties of window
, including name
, were made accessible from outside the origin, and for compatibility remain there today, with a whole load of complex caveats and limitations that have arisen from the endless stream of browser security holes that resulted.
(* It turns out not many people wanted to do that, but they did want to be able to pass string data between two windows on different domains. These days we would just use window.postMessage
which is designed for this explicit purpose, but back then the only way to do it was to use the only property that was read-write to both parties, name
. This was clumsy and limited but a number of existing web sites did it, making it very difficult to remove from the web platform.)
Upshot: putting stuff in window
can have unforeseen consequences, so try to keep global variables to an absolute minimum, and avoid existing properties like name
.