Javascript DOM onbeforeunload Event via HTML Tag onbeforeunload attribute

Introduction

This example demonstrates how to assign an "onbeforeunload" event to a body element.

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

View in separate window

<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">
<a href="https://www.java2s.com">Click here to go to java2s.com</a>

<script>
function myFunction() {//from w  w w  . j  av  a  2  s.  c  om
  return "Write something clever here...";
}
</script>

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



PreviousNext

Related