Category: Vanilla Javascript

Added: 21st of December 2022

Viewed: 624 times


Get the window width and height in Javascript

The script below displays the window width and window height dimensions.

When the window is resized the window dimensions are reloaded using the window.addEventListener resize function.

We assign window.innerWidth and window.innerHeight properties to variables and then use the document.write to print the dimensions on the screen.

Create a new file on your Desktop named windowDimensions.html, copy and paste the code below then save the file. Launch the file in your browser.

<script>
window.addEventListener('resize', function ()
{ window.location.reload(); });

let windowWidth = window.innerWidth;
let windowHeight = window.innerHeight;
document.write(+windowWidth+ "px X " +windowHeight+ "px");
</script>