Alert a message when navigating away from the page:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*from w ww .j av a 2 s .c om*/ <script> $(document).ready(function(){ $(window).unload(function(){ document.getElementById("demo").innerHTML = "Goodbye!"; }); }); </script> </head> <body> <p id="demo"></p> <p>When you click <a href="http://java2s.com">this link</a>, or close the window, an event will be triggered.</p> </body> </html>
The unload()
method was deprecated in jQuery version 1.8 and removed in version 3.0.
The unload event occurs when the user navigates away from the page.
The unload()
method handles the unload event.
$(selector).unload(function)
Parameter | Optional | Description |
---|---|---|
function | Required. | function to run when the unload event is triggered |