HTML event attribute onunload

The onunload attribute event is triggered when a page has unloaded by navigating to other page or by closing the browser window.

What's new in HTML5



<element onunload="script or Javascript function name">

Supported Tags


Browser compatibility

onunload No Yes Yes Yes Yes


<!DOCTYPE html>
function goodbye(){<!--   ww  w .j ava  2s. c  om-->
   alert("on unload");

<body onunload="goodbye()">

<p>Close this window or press F5 to reload the page.</p>


Click to view the demo