Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Web API DOM interfaces

I'm trying to learn more about Web APIs. Reading about them I see that among the wide variety of web API's there's the DocumentObjectModel API. Reading about the DOM specifications, I find a list of a lot of DOM interfaces. This makes me feel a little upset. In my ignorance, I think that an API should contain only one interface (only one set of functions and properties), here I find a list of interfaces...and that's the first thing that I don't understand.

Moreover, I usually interact with the DOM using notations like

document.getElementById("idname");
document.getElementByTagName("tagname");
etc ect

I see that some of these DOM interfaces have names that I already know, like "Document" or "Window", I use them (like in the previous two lines of code) but I use them not capitalized ("document" rather than "Document"). I don't understand if there is a link beetwen "document" and "Document", for example...

Finally, I noticed that querying for the global object (in the browser console, for example, simply typing the keyword "this") I have in response an object that (I guess) contains all the properties and functions (or methods) of the global object. And among them there are also functions that have the same names listed in the DOM interfaces list. And that's simply another thing that I noticed that I'm not able to explain.

So, can you help me to understand more deeply DOM API?

like image 731
marco Avatar asked Sep 05 '16 18:09

marco


1 Answers

Those things arise from an interaction of javascript and the DOM specifications.

The capital-letter Window and Document are interfaces defined by the DOM and HTML specs.

In Javascript, when executed in a browser environment, an instance of Window is the global object and Window is its prototype. document and window are getter properties defined by those interfaces. Since the window is the global object (the top-level this) and variable name resolution walks up the scope chain and finally lands at the global object if it cannot find it elsewhere window will eventually resolve to <global object>.window. Which is the attribute defined on the Window interface, which returns the window instance itself, which also is the global object. It is self-referential.

So the global object ends up having properties defined by javascript, by the dom/html specs and inherited from the prototypes also defined by those various specs.

Some prototypes are also exposed as named properties in the global object and not just the prototype chain of the instances. This is convenient for instanceof checks and feature detection. Some constructors are also exposed, e.g. new Image() creates a new instance of HTMLImageElement the same way document.createElement("img") would.

Additional Document instances can created via various constructors or factories such as DOMParser. Additional Window instances might be accessed through frames, the page's opener or spawned child windows.

Looking at the window object with a debugger may help.

like image 84
the8472 Avatar answered Oct 26 '22 21:10

the8472