Given an HTML document that is running on a Window. The task is to find the height and width of the window using JavaScript.
Example 1: This example uses window.innerHeight and window.innerWidth property to get the height and width of the window. The innerHeight property is used to return the height of the window and The innerWidth property is used to return the width of the window.
<!DOCTYPE HTML> <html> <head> <title> calculate Width and Height of the Window </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> Click Here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to display the window width and height --> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the" + " width and height of the Window"; function GFG_Fun() { var width = window.innerWidth; var Height = window.innerHeight; el_down.innerHTML = "Width: " + width + " pixels" + ", " + "Height: " + Height + " pixels"; } </script> </body> </html> |
Output:
Before clicking on the button:

After clicking on the button:

Example 2: This example uses document.documentElement.clientHeight and document.documentElement.clientWidth method to get the Height and Width of window respectively.
<!DOCTYPE HTML> <html> <head> <title> calculate Width and Height of the Window </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> Click Here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <!-- Script to display the device screen width --> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the" + " width and height of the window"; function GFG_Fun() { el_down.innerHTML = "Width:" +document.documentElement.clientWidth + " pixels"+", "+"Height:"+document.documentElement.clientHeight + " pixels"; } </script> </body> </html> |
Output:
Before clicking on the button:

After clicking on the button:

Recommended Posts:
- HTML width/height Attribute vs CSS width/height Property
- How to get the image size (height & width) using JavaScript ?
- Calculate the width of the text in JavaScript
- CSS | Height and Width
- How to get the width and height of an image ?
- How to adjust the width and height of iframe to fit with content in it ?
- How to animate div width and height on mouse hover using jQuery ?
- How to change the width and height of Twitter Bootstrap's tooltips?
- How to give a div tag 100% height of the browser window using CSS
- Javascript | Window Open() & Window Close Method
- Javascript | Window Blur() and Window Focus() Method
- How to get the width of scroll bar using JavaScript ?
- How to get the <div> height using JavaScript ?
- How to change the width of an iframe to 100% using JavaScript ?
- How to find the width of a div using vanilla JavaScript?
- How to get the width of device screen in JavaScript ?
- How to get the height of device screen in JavaScript ?
- How to Adjust the Width of Input Field Automatically using JavaScript ?
- How to set full-screen iframe with height 100% in JavaScript ?
- How to open URL in a new window using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


