简体   繁体   中英

Why is window.name cached?

in a programming challenge I recently took part in I had to use the window.name property to store / manipulate data. I found out that, when you change this property, it persists through page refreshes (although not when opening a new page with the same URL).

The only information I could find was that this is known and even used by some frameworks as data storage, but I would be interested in the why (as in why is window.name persistent? Any historical reasons?) and the how (which rules are there of when the window.name is kept between page changes and when it is discarded?).

Apparently, my Google-fu is not strong enough to find the answers to these questions (there is not even a mention of it on the MDN page!) so I hope that maybe you could help me.

My understanding of it is that the window object is persistent throughout the lifetime of a tab, and represents the window that is loading different HTML documents.

Each tab contains its own window object, which is why even when you navigate to/from different pages the window object is persistent, whereas if you check on a different tab the window.name will be different.

When opening different html pages, most of them do not override the window.name property, and it is completely optional. If nothing else is manipulating it, it will be what you leave it as. Most pages only touch on manipulating the window.document itself.

Named windows are used as link targets, for one:

<a href="example.html" target="some_page">some page</a>

The link will open in a new window once, and in the same window if it still exists on subsequent clicks, with the window's name being how it's targeted.

The second argument of window.open is also a window name.

window.open('example.html', 'some_page');

You can try it out in your browser across unrelated websites; in one tab's console, set window.name = 'test'; and in the other, use window.open('https://example.com/', 'test'); . (You may have to let it through a pop-up blocker.) The unrelated tab should navigate to https://example.com/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM