The employee showed me the following code and asked me why it worked.
<span id="myspan">Do you like my hat?</span>
<script type="text/javascript">
var spanElement = document.getElementById("myspan");
alert("Here I am! " + spanElement.innerHTML + "\n" + myspan.innerHTML);
</script>
I explained that the property is bound to a window object with the name of the element identifier when the browser parses a document that then contains a link to the corresponding dom node. This is similar to what window.myspan = document.getElementById("myspan")gets called backstage as the page is rendered.
In the following discussion, we raised several questions:
The window object and most of the DOM are not part of the official JavaScript / ECMA standards, but is the behavior described above documented in any other official literature, possibly related to the browser?
The above works in the browser (at least the main rivals), because there is a window object, but something like a rhino does not work out. Is code that relies on this erroneous practice because it makes too many assumptions about the runtime?
Are there any browsers in which the above errors will not be executed, or is this considered standard behavior in all directions?
Does anyone here know the answers to these questions and will be ready to enlighten me? I tried a quick search on the Internet, but I admit that I don’t know how to tell about the request correctly. Pointers to links and documentation are welcome.
source
share