DOM Window: resize event
The resize event fires on the window whenever the viewport changes size.
Description
The resize event fires when the viewport is resized, including when a phone is rotated. It can fire many times a second while dragging, so keep the handler light or throttle it. To react to breakpoints, matchMedia() is more efficient, and to watch a particular element's size, use ResizeObserver.
A simple way to limit work is to schedule it with requestAnimationFrame() or a short setTimeout() debounce, so heavy calculations run at most once per frame or once the user stops resizing. Mobile browsers can fire resize when the address bar shows or hides, so don't assume it only fires when the window is dragged.
Listeners for window events are added with window.addEventListener(). The older way, assigning a function to a property such as window.onresize, only allows one handler, so a second assignment replaces the first.
Syntax
window.addEventListener('resize', handler)Example
Browser Support
The following information will show you the current browser support for the Window: resize event event. Hover over a browser icon to see the version that first introduced support for this event.
Desktop





Tablets & Mobile





