Categories
JavaScript Answers

How to detect when a window is resized using JavaScript?

Spread the love

Sometimes, we want to detect when a window is resized using JavaScript.

In this article, we’ll look at how to detect when a window is resized using JavaScript.

How to detect when a window is resized using JavaScript?

To detect when a window is resized using JavaScript, we listen for the resize event on window.

For instance, we write

window.addEventListener("resize", onResize);

to call window.addEventListener to listen to the resize event triggered by window.

The onResize function is the resize event listener and it runs every time the window is resized.

Conclusion

To detect when a window is resized using JavaScript, we listen for the resize event on window.

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 *