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?
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 Document
s, for example an XML document obtained via ajax), with methods like getElementById
or addEventListener
.
A very detailed explanation: Read here
Basically window is your browser's window and document is the HTML page inside it.
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