The following code shows an Example of Onload and OnBeforeUnload in JavaScript. As can be seen the following ite a JavaScript code block, greets a user with a message Welcome’. When a user leaves this page, the alert dialog box is displayed.
<html>
<head>
<title>OnBeforeUnload Example</title>
<script>
function f1()
{
alert("Welcome to JavaScript Event Handling!");
}
function f2(){ return "bye...";}
</script>
<style>
body{
text-align: center;
font-size: 30px;
background: #ee6699;
padding: 30px;
}
</style>
</head>
<body onload="f1();" onbeforeunload="return f2();">
<h1>JavaScript OnLoad and OnBeforeUnload Event Handling!</h1>
<a href="https://www.programmingempire.com" target="_blank">Programmingempire</a>
</body>
</html>
