Categories
JavaScript Answers

How to Detect When a Window is Resized Using JavaScript?

Spread the love

We can detect when a window is resized with jQuery.

For instance, we can write:

$(window).resize(() => {  
  console.log('resized')  
});

to watch the resize event with jQuery on window .

window is the current browser tab.

The callback runs when we resize the window, so 'resized' will be logged when we resize it.

Detect When a Window is Resized Using JavaScript

We can detect when a window is resized with JavaScript.

To do this, we write:

window.addEventListener('resize', () => {  
  console.log('resized')  
});

We listen to the resize event with the addEventListener on the window .

And we have the same callback as the 2nd argument which runs when the resize event is triggered.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *