/

HTML / CSS / JavaScript Tutorial

HTML Event attribute: onbeforeunload

[this page | pdf | back links]

The HTML onbeforeunload attribute specifies the event that is triggered just before a document is unloaded. It applies to <body> elements.

 

It can be used to return a message if the user is just about to leave the page.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body onbeforeunload="return myOnbeforeunload()">
onbeforeunload: try leaving page,
e.g. by following this link <a href="http://www.nematrian.com">nematrian homepage</a><br>

<script>
function myOnbeforeunload() {return "Page about to be unloaded";}
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Attributes


Desktop view | Switch to Mobile