Course
Window Object
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Window Object
The JavaScript window object represents the browser's window. In JavaScript, a 'window' object is a global object. It contains the various methods and properties that we can use to access and manipulate the current browser window. For example, showing an alert, opening a new window, closing the current window, etc.
All the JavaScript global variables are properties of window object. All global functions are methods of the window object. Furthermore, when the browser renders the content in the 'iframe’, it creates a separate 'window' object for the browser and each iframe.
Here, you will learn to use the 'window' object as a global object and use the properties and methods of the window object.
Window Object as a Global Object
As 'window' is a global object in the web browser, you can access the global variables, functions, objects, etc., using the window object anywhere in the code.
Let's understand it via the example below.
Example
In the below code, we have defined the 'num' global and local variables inside the function. Also, we have defined the 'car' global object.
In the test() function, we access the global num variable's value using the 'window' object.
<html><body> <div id = "output1">The value of the global num variable is: </div> <div id = "output2">The value of the local num variable is: </div> <div id = "output3">The value of the car object is: </div> <script> var num = 100; const car = { brand: "Honda", model: "city", }
function test() { let num = 300; document.getElementById("output1").innerHTML += window.num; document.getElementById("output2").innerHTML += num; document.getElementById("output3").innerHTML += car.brand; } test(); </script></body></html>
Output
The value of the global num variable is: 100The value of the local num variable is: 300The value of the car object is: Honda
You may also use the 'window' object to make a particular variable global from a particular block.
Window Object Properties
The 'window' object contains the various properties, returning the status and information about the current window.
Below, we have covered all properties of the 'window' object with a description. You may use the 'window' as a reference to access these properties.
Here, we will cover some properties with examples.
OuterHeight/OuterWidth Properties of the Window object
The outerHeight property of the window object returns the window's height, and the outerWidth property of the window object returns the window's width.
Example
In the code below, we used the outerHeight and outerWidth property to get the dimensions of the window. You can change the size of the window and observe changes in the value of these properties.
<html><body> <p id = "output1">The outer width of the window is: </p> <p id = "output2">The outer height of the window is: </p> <script> const outerHeight = window.outerHeight; const outerWidth = window.outerWidth; document.getElementById("output1").innerHTML += outerWidth; document.getElementById("output2").innerHTML += outerHeight; </script></body></html>
Output
The outer width of the window is: 1536The outer height of the window is: 816
ScreenLeft Property of the Window Object
The window screenLeft property returns the left position of the current window.
Example
In the output of the below code, you can see the left position of the current window in pixels.
<html><body> <div id = "output">Your browser window is left by: </div> <script> const screenLeft = window.screenLeft; document.getElementById("output").innerHTML += screenLeft + " px."; </script></body></html>
Output
Your browser window is left by: 0 px.
Window Object Methods
The 'window' object also contains methods like properties to manipulate the current browser window.
In the below table, we have covered the methods of the 'window' object with their description. You may use 'window' as a reference to access and invoke the below methods to make the code readable.
Here, we will cover some methods with examples.
JavaScript window.alert() Method
The
window.alert()
method allows you to show the pop-up dialog containing the message, warning, etc. It takes the string text as an argument.Example
In the below example, when you click the button, it will invoke the alert_func() function and show the pop-up box at the middle top.
<html><body> <button onclick = "alert_func()"> Execute Alert </button> <script> function alert_func() { window.alert("The alert_func funciton is executed!"); } </script></body></html>
JavaScript window.open() Method
The
window.open()
method is used to open a new window. It takes a URL as an argument, which you need to open in a new window.Example
In the below code, we used the window.open() method to open a new window in the browser. You can see that the code opens the home page of the 'tutorialspoint' website in the new window.
<html><body> <button onclick = "openWindow()"> Open New Window </button> <script> function openWindow() { window.open("https://www.tutorialspoint.com/"); } </script></body></html>
JavaScript window.print() Method
The window.print() method lets you print the web page.
Example
In the below code, click the button to print the web page.
<html><body> <h2> Hello World! </h2> <button onclick = "printPage()"> Print Webpage </button> <script> function printPage() { window.print(); } </script></body></html>