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.
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.
Share Improve this question asked May 6, 2015 at 14:55 user4853991user4853991 951 silver badge8 bronze badges 2- 1 Think of the document as being inside the window. The window is where all of the page's global properties reside. – Robusto Commented May 6, 2015 at 14:57
- Your question should be "i wanna know whats the difference between document && window in JavaScript ??“, as both objects e from vanilla JS, not jQuery – Reeno Commented May 6, 2015 at 15:44
3 Answers
Reset to default 11Phew . . . 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., 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:
- the
window
object - https://developer.mozilla/en-US/docs/Web/API/Window - the
document
object - https://developer.mozilla/en-US/docs/Web/API/document
One last note: While not pletely 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./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.
The window is the first thing that gets loaded into the browser. This window object has the majority of the properties like length, innerWidth, innerHeight, name, if it has been closed, its parents, and more. What about the document object then?
The document object is your html, aspx, php, or other document that will be loaded into the browser. The document actually gets loaded inside the window object and has properties available to it like title, URL, cookie, etc. What does this really mean? That means if you want to access a property for the window it is window.property, if it is document it is window.document.property which is also available in short as document.property.
For More detail with screenshot read following article
http://eligeske./jquery/what-is-the-difference-between-document-and-window-objects-2/