Skip to content Skip to sidebar Skip to footer

Document.onclick Vs Window.onclick

Is there any difference between document.onclick and window.onclick event? Thanks.

Solution 1:

The JavaScript Window object is the highest level JavaScript object which corresponds to the web browser window.

The document object is the container for all HTML HEAD and BODY objects associated within the HTML tags of an HTML document. This could correspond to the top-most window, or an iframe within the window.

Update

After a quick test there really is no difference between the two. However, as others have said, window.onclick did not work when tested in IE8. So apparently the bottom line is that document.onclick is the preferred choice.

Solution 2:

I've heard of some versions of IE not supporting window.onclick

Solution 3:

The w3c describes the document as: "The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data." (http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document)

and window as: "...defines the Window object, which provides the global namespace for web scripting languages, access to other documents in a compound document by reference, navigation to other locations, and timers. The Window object is a long-standing de facto standard for HTML user agents. However, it should not be assumed based on this or the name "Window" that it is limited to HTML or to visual user agents." (http://www.w3.org/TR/Window/)

So to answer your question, depending on the browser there might not be any issue. But some browser vendors might implement them differently.

Solution 4:

If there are any differences at all, I am not aware of any. I believe in the end the document references the window.

Post a Comment for "Document.onclick Vs Window.onclick"