The onresize attribute is an event-handler attribute for the window resize event. It runs JavaScript when the document view (browser window) has been resized. In HTML, place it on <body> — that maps to the Window object, same as window.onresize.
The event is not cancelable and does not bubble. Modern browsers fire resizeonly on window. For an element’s own size changes, use ResizeObserver instead. Related window UI events: onscroll and onload.
Baseline · Widely available.
Not deprecated and not experimental. The window resize event is stable across modern browsers since about July 2015.
Prefer addEventListener in apps.
Inline onresize is fine for learning. Production code usually uses window.addEventListener("resize", handler). Debounce heavy work — resize can fire many times while dragging.
Syntax
HTML Syntax
syntax.html
<body onresize="script">
script is JavaScript that runs when the window is resized. Equivalent JS: window.onresize = handler or window.addEventListener("resize", handler).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Run heavy work once — layout, charts, or network — not on every pixel drag.
Keep the page responsive — debounce is the usual pattern for window resize.
Edge Cases & Pitfalls
Most confusion is listening on a div, or doing heavy work on every resize tick.
window only
Fires only on window
You can set onresize on other elements, but modern browsers deliver resize only to the window. Use ResizeObserver for boxes.
body
<body onresize> maps to Window
In HTML, several body handlers (including onresize) are window event-handler content attributes — not element-level resize.
performance
Fires very often while dragging
Debounce or throttle before expensive layout, canvas, or network work. See MDN’s notes on optimizing window.onresize.
iframe
Embedded demos report the iframe
In Try It Yourself, sizes are for that frame’s viewport. Open the result fullscreen to feel a real window resize.
property
.onresize = replaces listeners
Assigning the IDL property overwrites a previous assignment. Prefer addEventListener for multiple handlers.
Compatibility
Browser Support
onresize is a window event-handler attribute (often written on body in HTML). It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onresize
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the window resize event.
97%Global support (approx.)
Google Chrome1+
Full support
Mozilla Firefox1+
Full support
Apple Safari1.1+
Full support
Microsoft Edge12+
Full support
Opera7+
Full support
Samsung InternetModern · Full
Full support
Internet Explorer4+
Supported
onresizeWidely supported
Bottom line: Safe to use for viewport-size UI. Prefer addEventListener, debounce heavy work, and use ResizeObserver for element-level size tracking.
Remember
Key Takeaways
Role: run script when the browser window is resized.
Target:window only — use body onresize or addEventListener.
Read size:innerWidth / innerHeight (or CSS media queries for layout).
Elements: track box size with ResizeObserver, not onresize.
One line:onresize means “the window changed size” — listen on window, debounce heavy work, and use ResizeObserver for elements.
Frequently Asked Questions
It is an event-handler attribute for the window resize event. When the document view (browser window) is resized, the resize event fires and the script runs. In HTML, putting onresize on body maps to the Window object.
On the body element in markup (it maps to window), or use window.addEventListener("resize", handler) in JavaScript. Resize events fire only on the window, not on arbitrary elements.
No. Modern browsers fire resize only on window. For a box or component changing size, use the ResizeObserver API instead.
Dragging a window edge can fire many times per second. Debounce or throttle the handler if you do heavy work (layout, network, canvas redraw).
Both work. For learning, body onresize is fine. In production code, prefer window.addEventListener("resize", handler).
No. The resize event and onresize handler are Baseline widely available. Prefer addEventListener in apps, and ResizeObserver for element-level size changes.