i wanna know whats the difference between document && window in jQuery ??
These two are used quite often, but i hv never got the difference between them.
Phew . . . that's actually a lot bigger of a question than you may realize. :)
The Extremely Short Answer is . . .
The window
object represents the container that the document
object is displayed in. In fact, when you reference document
in your code, you are really referencing window.document
(all properties and methods of window
are global and, as such, can be referenced without actually specifying window
at the beginning . . . e.g., document
= window.document
and alert()
= window.alert()
).
The document
object is the currently loaded DOM document . . . so, if you go to http://www.stackoverflow.com, the document
object would be all of the HTML, JS, CSS, etc. that are loaded to make up the StackOverflow home page. If you click on the link to this question, the document
is now all of the same kinds of assets that make up the page for this question. When you change documents
though, you are still in the same window
(though some of the properties of the window
have changed).
For LOTS of information on the two objects (including standard properties and methods), check out these links:
window
object - https://developer.mozilla.org/en-US/docs/Web/API/Window document
object - https://developer.mozilla.org/en-US/docs/Web/API/document
One last note: While not completely accurate, if you are a visual person, you can think of the window
as the browser window or tab that you have open to view web pages . . . you may move through many document
s as you are surfing, but, if you never change to a different tab, you are always in the same window
.
This article explain benefits of both
http://web.enavu.com/daily-tip/daily-tip-difference-between-document-ready-and-window-load-in-jquery/
In short term: window
- you can handle if user interact with window (open, close, etc..)document
- is a content of window and you can handle if user iteract with content (watched, fired some events like a click, change etc)
But keep in mind !! They are different objects and does different things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With