Wednesday, April 23, 2025

JavaScript Get Window Size

You can get the size of the current window using the window.innerWidth and window.innerHeight properties. These properties return the width and height of the viewport, which is the visible area of the browser window. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	var wid = window.innerWidth;
	var hei = window.innerHeight;
	
	document.write("<h1>" + wid + "</h1>");
	document.write("<h1>" + hei + "</h1>");
	
</script>
	
</body>
</html>

This script gets the width and height of the current window using the window.innerWidth and window.innerHeight properties and then displays them as headings using the document.write() method.

When you run this script in a browser, it will display the width and height of the window as headings on the page.

The window.innerWidth and window.innerHeight properties are useful for many purposes in JavaScript, such as:

  1. Responsive web design: You can use these properties to create responsive web designs that adjust to the size of the user's screen.

  2. DOM manipulation: You can use the window size to dynamically adjust the size and position of HTML elements on a page based on the size of the viewport.

  3. Game development: You can use the window size to set the boundaries of a game's playing area or to adjust the game's graphics and controls based on the size of the user's screen.

  4. User experience: You can use the window size to optimize the layout and design of a website or application to provide the best user experience on different devices and screen sizes.

The window.innerWidth and window.innerHeight properties are essential tools for creating dynamic, responsive, and user-friendly web applications in JavaScript.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...