views:

561

answers:

4

Is there any difference between

document.onclick

and

window.onclick

event?

Thanks.

+1  A: 

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

Anthony Forloney
window.onclick doesnot work in IE8 whereas works in FireFox
Hoque
+1  A: 

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

KThompson
I tested and found that window.onclick does not work in IE8. I did not test it in other version of IE. However window.onclick works in FireFox.
Hoque
If it doesn't work in IE8 I'd put money that it doesn't work in IE at all
KThompson
+2  A: 

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.

Justin Ethier
+1  A: 

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.

BenMills