addEventListener("beforeunload", myFunction); - Javascript DOM Event

Javascript examples for DOM Event:addEventListener

Introduction

The onbeforeunload event occurs when the document is about to be unloaded.

Summary

Bubbles No
Cancelable Yes
Supported HTML tags: ?<body>

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<a href="https://www.java2s.com">Click here</a>

<script>
window.addEventListener("beforeunload", function(event) {
    event.returnValue = "Write something clever here..";
});/*www . ja  v  a  2s  . com*/
</script>

</body>
</html>

Related Tutorials