Code has been added to clipboard!

Tutorial on JavaScript Window Object With Examples

Reading time 3 min
Published Aug 8, 2017
Updated Oct 1, 2019

It is important to learn how JavaScript interacts with browsers. To communicate with the browser, JavaScript uses the browser object model (BOM). It deals with browser components (history, location, navigator and screen). Document is a property of BOM.

In this tutorial, you will learn all about the JavaScript windows object, get a better idea of what it is and how you can work with it. You will learn about its methods and how you can use them.

JavaScript Window: Main Tips

  • The Browser Object Model (BOM) helps JavaScript to communicate with the browser.
  • Using the methods and properties of a window JavaScript offers, you can modify a particular window and get information on it.

The Window Object

All browsers support the object window JavaScript uses. It defines the window of the browser. Global JavaScript objects, functions, and variables become part of the window object automatically.

All global variables are window object properties, and all global functions are its methods. The whole HTML document is a window property, too.

In the example below, you can see how the document object (which belongs to the HTML DOM) is in the window object's property. Here, we select the body of the document:

Example
window.document.getElementById("body");

This next example does the exact same thing. However, it is used to show that the window is not necessary for the function to work:

Example
document.getElementById("body");

BOM vs. DOM

For a beginner, it's easy to get confused and lost among various terms, especially the ones that look a lot alike. Therefore, we thought we should clarify once again what is the main difference between BOM and DOM.

BOM (browser object model) allows JavaScript to communicate with the browser and its components, like history, location, navigator, screen and document.

DOM (document object model) actually represents the document: all of the HTML elements, events, etc. Thus, DOM is part of the BOM.

Getting Window Size

Height and width properties can be used to return the browser's window size. Both of the properties return the size in pixels:

  • window.innerHeight returns the height of the browser's window.
  • window.innerWidth returns the width of the browser's window.

Keep in mind we use different properties for Internet Explorer 8 and older versions:

  • document.documentElement.clientHeight
  • document.documentElement.clientWidth

or

  • document.body.clientHeight
  • document.body.clientWidth

The example below displays a universal code that works on any browser:

Example
var w = window.innerWidth
|| document.body.clientWidth
|| document.documentElement.clientWidth;

var h = window.innerHeight
|| document.body.clientHeight
|| document.documentElement.clientHeight;

Note: the window property returns the browser window's height and width excluding toolbars.

Browser Methods

There are some more methods you may use with JavaScript windows objects:

  • JavaScript window.open() is used to open a new window.
  • window.close() is used to make JavaScript close window.
  • By using window.moveTo(), you can move the current window.
  • You should choose window.resizeTo() to resize the current window.

JavaScript Window: Summary

  • JavaScript uses the browser object model (BOM) to communicate with the browser.
  • BOM has various methods and properties that come useful when working with windows. For example, JavaScript window.open is used to open a new one.
  • DOM is part of the BOM.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen