0

In the W3C Web Storage (Second Edition) document, the recommendation always say "Document's Window object" or "Document object's Window object" rather than "Window object's Document object".

But as we all know we use window.document in javascript.

What's the difference between "Document" in the recommendation and the window.document object we always use in javascript? Why not "Window object's Document object"?

MichaelLi
  • 3
  • 1
  • `Why not "Window object's Document object"?` because that would really be `Why not "Window object's document object"?` (lower case d as it's window.document not window.Document - I can only guess the Document isn't meant to be referring to `window.document` – Jaromanda X May 17 '17 at 04:00
  • I'm not a native English speaker, but isn't *"Document"* the subject in your sentence *"Window object's Document object"* while *"Window"* is the one of W3C's in *"Document's Window object"* and *"Document object's Window object"* ? Or did you mean *why not "Window object's Document object's Window object" ?* ? In this case, because recursive objects lead to inifinite loops. – Kaiido May 17 '17 at 04:48
  • Also note that they use *"Document's Window's object"* because e.g a framed document's Window object will not be the global `window` of the main document. And finally these are DOM, not js. – Kaiido May 17 '17 at 04:55
  • Regarding "*What's the difference between "Document" in the recommendation and the window.document object we always use in javascript?*", see [What is the difference between Document and document in JavaScript?](http://stackoverflow.com/q/16790174/1048572) – Bergi May 17 '17 at 05:06
  • Window objects belong to documents, not the other way around. A browsing context has a history of documents, one of which is the current document, but each of which has its own window object. This history can be navigated through using the browser's back and forward buttons. When you use to the global object `window`, you're really using something called a WindowProxy object, which passes all its property accesses on to the window object contained by the current document. – Alohci May 18 '17 at 08:19

1 Answers1

0

This is because the (session) storage is associated with the document, not the window. Or more specifically, it is initialised depending on the origin of the current document:

https://www.w3.org/TR/webstorage/#the-sessionstorage-attribute

When a new Document is created in a browsing context which has a top-level browsing context, the user agent must check to see if that top-level browsing context has a session storage area for that document's origin. If it does, then that is the Document's assigned session storage area. If it does not, a new storage area for that document's origin must be created, and then that is the Document's assigned session storage area. A Document's assigned storage area does not change during the lifetime of a Document.

Of course, that every window has its document and every document has its window1 is implied only and stated nowhere in the WebStorage spec, nor is accessing the relation made explicit.

So when they say "Document's Window object", they refer to the document's browsing context, accessible as document.defaultView, which are defined in the HTML spec (right next to window.document).

1: which isn't even completely true, they might have associated documents/windows, but in all contexts where we access a storage they will.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375