What is the difference between window.element and element when accessing DOM elements

I like to understand how the browser distinguishes hi and window.hello in the code below

http://jsfiddle.net/PH3t2/291/

var hello = "new hello";
console.log("variable hello : " + hello); // <-- prints "new hello"
console.log(window.hello); // <-- logs HTML elements
<div class="mainWrapper">
  <div class="mainBox" id="hello">
    main
  </div>
  <div class="clear" id="hello"></div>
</div>
Run codeHide result

How does a given window print HTML elements, not a string "new hello"?

+6
source share
1 answer

The problem is that, by default, the browser saves all elements as properties windowthat are bound to their attribute id- this is part of the reason you cannot have multiple elements with the same id, so the HTML that you specified is not valid.

window.hello Element - <div> HTML.

, , hello, , , window.hello. hello "new hello".

+6

All Articles