How to detect when the window size is resized using JavaScript ?
Sometimes when we develop our site, we want to detect the size of the window, In this article, we are going to learn how to detect when the window size is resized using JavaScript
The window resize event occurs whenever the size of the browser window gets changed. We can listen to the resize event in two ways:
Method 1: Using resize event
We can add an event listener to the body element which fires whenever the window size is resized.
Example: This example shows the use of the above-explained approach.
<!DOCTYPE html>
<html>
<head>
<title>
How to detect and resize
the window size
</title>
<style>
body {
text-align: center;
}
h1 {
color:green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
Resizing the screen size and display its
<br>corresponding width and height
</h3>
<div id="height">Height: <span></span></div>
<div id="width">Width: <span></span></div>
<script>
const height = document.querySelector("#height span");
const width = document.querySelector("#width span");
// Insert values on load of page
window.onload = function() {
height.innerHTML = window.innerHeight;
width.innerHTML = window.innerWidth;
};
// Change values when window is resized
window.onresize = function() {
// Setting the current height & width
// to the elements
height.innerHTML = window.innerHeight;
width.innerHTML = window.innerWidth;
};
</script>
</body>
</html>
Output:
Method 2: Using ResizeObserver API
We can use the latest ResizeObserver API to listen to the window resize event. This API is not fully supported.
Example: In this example, we are using ResizeObserver API.
<!DOCTYPE html>
<html>
<head>
<title>
How to detect and resize
the window size
</title>
<style>
body {
text-align: center;
}
h1 {
color:green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
Resizing the screen size and display its
<br>corresponding width and height
</h3>
<div id="height">Height: <span></span></div>
<div id="width">Width: <span></span></div>
<script>
const body = document.getElementsByTagName("body")[0];
const height = document.querySelector("#height span");
const width = document.querySelector("#width span");
// Initialize resize observer object
let resizeObserver = new ResizeObserver(() => {
// Set the current height and width
// to the element
height.innerHTML = window.innerHeight;
width.innerHTML = window.innerWidth;
});
// Add a listener to body
resizeObserver.observe(body);
</script>
</body>
</html>
Output:
Method 3: Using addEventListener()
Method
The addEventListener
method in JavaScript is used to attach an event handler to a specified element, enabling the execution of a function or a piece of code in response to a specific event.
Example: The updateSize
function is created to update the height and width based on the window size. The addEventListener
method is used to attach the resize
event to the window
object. When the window is resized, the updateSize
function is called to update the displayed height and width. The initial values are set by calling updateSize
once when the page loads.
<!DOCTYPE html>
<html>
<head>
<title>
How to detect and resize the window size
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
Resizing the screen size and display its
<br>corresponding width and height
</h3>
<div id="height">Height: <span></span></div>
<div id="width">Width: <span></span></div>
<script>
const height = document.querySelector("#height span");
const width = document.querySelector("#width span");
// Function to update height and width
function updateSize() {
height.innerHTML = window.innerHeight;
width.innerHTML = window.innerWidth;
}
// Add event listener for window resize
window.addEventListener('resize', updateSize);
// Initial update
updateSize();
</script>
</body>
</html>
Output: