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.

   <title>OnBeforeUnload Example</title>
     function f1()
          alert("Welcome to JavaScript Event Handling!");
      function f2(){ return "bye...";}
       text-align: center;
       font-size: 30px;
       background: #ee6699;
       padding: 30px;
 <body onload="f1();" onbeforeunload="return f2();">
    <h1>JavaScript OnLoad and OnBeforeUnload Event Handling!</h1>
    <a href="" target="_blank">Programmingempire</a>