Events

onresize

The window's document viewport size changes. Viewport resize targets Window; the body attribute can reflect the Window handler. A div does not receive ordinary viewport resize events. Event interface: Event. For the described native event: bubbles: no; cancelable: no. For individual element dimensions use ResizeObserver; avoid expensive work on each resize event. Example: Resize the standalone browser viewport. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
<body onresize="handleEvent(event)">  window.addEventListener("resize", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
onresizeJavaScript handler bodyOptional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute.
eventEventEvent object delivered to the addEventListener callback; event.type is "resize". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for resize</p><script>  const target = window;  const log = document.getElementById("log");  target.addEventListener("resize", event => {    log.textContent = String(innerWidth + " × " + innerHeight);  });</script>
HTML Reference