Up to $140 off-Lowest  Prices of 2024(Nov.21-Dec.2)

My Computer Tips

Home | About | Categories | All Tips & Tutorials

Get the window width and height in Javascript

ID: 330

Category: Vanilla Javascript

Added: 21st of December 2022

Views: 1,232

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>