Browser Window Vs Document
Demistifying the difference.
author
Written byLakshit NagarPrinciple Software Developer@Oracle India (Ex-ThoughtWorker, IIT Kanpur Alumni)
Browser Window Vs Document
Demistifying the difference.
author
Written byLakshit NagarPrinciple Software Developer@Oracle India (Ex-ThoughtWorker, IIT Kanpur Alumni)

Intended Audience

This article is written by a software developer for anyone who is interested in the technical aspects of modern web browsers. To understand this article, readers need to know the overview of window and document.

Content

  1. Introduction
  2. Top level structure of window & document
  3. Scope of window & document
  4. Uses of window & document


Introduction

There are lot of misconception about what window and document is. Not only these two, there are other related concepts in browser/javascript which creates similar confusions. This article is an attempt to resolve all those misconceptions or confusions and give you a clear understanding of both.


Top level structure of window & document

window is an object which is created by the browser at runtime as soon as website loads. Browser then makes this object, accessible to the javascript engine. This window object contains various properties which controls/access different parts of the browser (visible/hidden). For example:

window.location  // controls browser's address bar
window.navigator  // fetches browser's geolocation
...
And one these property is the .document
window.document  // controls browser's web page
window and everything inside it is not defined by ECMAScript (javascript) but created by browser and sent to the javascript to control/access different parts of the browser. This is the reason, we don't see window object in NodeJS or related server side JS.
In a perfect analogy, we can compare it with Marvel's inifinity gaunlet. The gaunlet itself is of no use, but it can be used to harness powers of different infinity stones. Browser Window vs Document Gaunlet Similarly, window object is a gaunlet and all the properties inside are different inifinty stones. .document is a type of infinity stone inside the window object, which controls the webpage loaded in the browser.
In javascript universe there are way more then six infinity stones. :-)

Scope of window & document

Scope of any entity is the space in which it's presence is recognizable. The scope of the window object is vast. It can reach almost everywhere in the browser (list of window properties). It is so vast that sometimes it is referred as the global object. The document on the other hand, is subset of or contains inside the window object. Therefore, its scope is limited to the smaller space. In this space, document controls the webpage.

Browser Window vs Document Scope

Uses of window & document

window (window.location)

Browser's Address Bar
This property contains a Location object which exposes the browser's location api to get/set address bar of the current tab/iframe. Browser Window Location Object Console

Further, under window.location, we have multiple properties which represents/returns the different parts of the url in the address bar. This is explained with the help of below diagram. Browser Location API - URL Split If port is 80 or 443, host and hostname will be equal (omitting default port).

document (window.document)

Browser's Web Page
This property contains a Document object which exposes the browser's web page to get/set HTML element inside the current tab/iframe. Browser Window Document Object console

Under window.document, we have various functions using which we can change whatever we wnat on the said web page (eg. www.google.com).
For example, we can try changing "Google Search" text on google's web page.

window.document.getElementsByClassName('gNO89b')[1].value = "Hello World!";
Browser Document Change Google Search Text


Links & References

  1. https://developer.mozilla.org/en-US/docs/Web/API/Window

About Author

author
Lakshit Nagar (A full stack enthusiast)
Principle Software Developer
@Oracle India (Ex-ThoughtWorker, IIT Kanpur Alumni)

I love to shape my ideas into a reality. You can find me either working on a project or having a beer with my close friend. :-)

Connect: