Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between window and document in jQuery

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.

like image 329
user4853991 Avatar asked Dec 04 '22 03:12

user4853991


2 Answers

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:

  • the window object - https://developer.mozilla.org/en-US/docs/Web/API/Window
  • the 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 documents as you are surfing, but, if you never change to a different tab, you are always in the same window.

like image 158
talemyn Avatar answered Dec 09 '22 15:12

talemyn


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.

like image 43
daremachine Avatar answered Dec 09 '22 14:12

daremachine