Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to understand the difference between window and document objects in js [duplicate]

Tags:

javascript

I am trying to understand the difference between window and document objects in js. I checked online, but I still don't have a clear understanding. From what I know: window is like a super document, it includes the document object. So if I use firefox to open one page: localhost/test.js, can I say the browser:firefox is window object, and the file test.js is the document object?

like image 432
user2243528 Avatar asked Jun 21 '13 02:06

user2243528


2 Answers

The window object represents the current browsing context. It holds things like window.location, window.history, window.screen, window.status, or the window.document. Also, it has information about the framing setup (the frames, parent, top, self properties), and holds important interfaces such as applicationCache, XMLHttpRequest, setTimeout, escape, console or localStorage. Last but not least it acts as the global scope for JavaScript, i.e. all global variables are properties of it.

In contrast, the (window.)document object represents the DOM that is currently loaded in the window - it's just a part of it. A document holds information like the documentElement (usually <html>), the forms collection, the cookie string, its location, or its readyState. It also implements a different interface (there might be multiple Documents, for example an XML document obtained via ajax), with methods like getElementById or addEventListener.

like image 66
Bergi Avatar answered Sep 22 '22 23:09

Bergi


A very detailed explanation: Read here

Basically window is your browser's window and document is the HTML page inside it. enter image description here

like image 22
Junkster Avatar answered Sep 21 '22 23:09

Junkster