x
 
<!DOCTYPE html>
<html>
<head>
    <script>
        function beforeFunc(){
            alert("Example #2 says: 'You are leavng this page!!'");
        }
        
        function retFunc(){
            alert("Example #2 says: 'You closed the page...Hope you have read all the content of this page carefully.'");
        }
</script>
</head>
<body id="main" onbeforeunload="beforeFunc()" onunload="retFunc()">
    <p>Try reloading or closing the browser...</p>
    
</body>
</html>