Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onresize — resize event handler attribute

The onresize attribute contains JavaScript that handles a resize event. On the body element, it targets Window rather than changes to the size of the body element itself.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<body onresize="JavaScript statements">

onresize is an event handler attribute for resize; its value is JavaScript code, not a Boolean value. The handler code has access to the event parameter. A JavaScript property with the same name corresponds to the attribute.

On the body element, the attribute handles events on Window. Consequently, document.body.onresize and window.onresize refer to the same handler mechanism: assigning a new handler replaces the previous one. This should not be confused with observing the dimensions of body.

Although HTML also exposes onresize on other elements, the attribute's presence does not itself cause events whenever their dimensions change. In the example, the button dispatches a synthetic resize event to window to test the handler without manually resizing the window. This does not resize the window.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>onresize demo</title>
</head>
<body onresize="document.getElementById('result').textContent = event.type">
  <p id="result">ready</p>
  <button type="button" onclick="window.dispatchEvent(new Event('resize'))">Test</button>
</body>
</html>

Result

resize

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources