onresize event

Definition and usage

The onresize event occurs when the browser window is resized.

Tip:To get the size of an element, use clientWidth, clientHeight,innerWidth,innerHeight,outerWidth,outerHeightand offsetWidth and/or offsetHeight properties.

Example

Example 1

Execute JavaScript when the browser window size is adjusted:

<body onresize="myFunction()">

Try it yourself

Example 2

Use the addEventListener() method to attach the "resize" event to the window object:

window.addEventListener("resize", myFunction);

Try it yourself

Syntax

In HTML:

<element onresize="myScript">

Try it yourself

In JavaScript:

object.onresize = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("resize", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Not supported
Cancelable: Not supported
Event type: If generated from the user interfaceUiEvent. Otherwise Event.
Supported HTML tags: <body>
DOM version: Level 2 Events

Browser support

Events Chrome IE Firefox Safari Opera
onresize Support Support Support Support Support