Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.onclick vs window.onclick

Is there any difference between

document.onclick 

and

window.onclick 

event?

Thanks.

like image 368
Nazmul Avatar asked Mar 05 '10 02:03

Nazmul


People also ask

Is onClick and onClick same?

onClick will work in html, but if you're defining the handler in JS, you need to use the lowercased onclick. in XHTML, HTML attributes are case sensitive.

What is document onClick JavaScript?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.

What does the window onClick event handler do in this HTML document?

The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you change certain content on the web page, and other things like that. You place the JavaScript function you want to execute inside the opening tag of the button.

What is window in JavaScript?

A global variable, window , representing the window in which the script is running, is exposed to JavaScript code. The Window interface is home to a variety of functions, namespaces, objects, and constructors which are not necessarily directly associated with the concept of a user interface window.


2 Answers

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.

like image 99
Justin Ethier Avatar answered Sep 21 '22 23:09

Justin Ethier


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

like image 29
KTastrophy Avatar answered Sep 18 '22 23:09

KTastrophy