Javascript DOM onbeforeunload Event via onbeforeunload function

Introduction

In JavaScript:

object.onbeforeunload = function(){
       myScript};

This example uses the HTML DOM to assign an "onbeforeunload" event to the window object.

Close this window, press F5 or click on the link below to invoke the onbeforeunload event.

View in separate window

<!DOCTYPE html>
<html>
<body>
<a href="https://www.java2s.com">Click here to go to java2s.com</a>
<script>
window.onbeforeunload = function(event) {
  event.returnValue = "Write something clever here..";
};
</script>/*from  www.  j  av a2s .c o  m*/

</body>
</html>
Bubbles: No
Cancelable: Yes
Event type: UiEvent if generated from a user interface, Event otherwise
Supported HTML tags: <body>



PreviousNext

Related