Skip to content
Advertisements

JavaScript onbeforeunload Event

Why onbeforeunload ?

The onbeforeunload property of the JavaScript works when you are prompted to remove the document. This is generally used in today’s websites. As he leaves a page, he asks whether he really wants to leave. It does this with the confirmation dialog box.
The checkboxes are different in different browsers:


onbeforeunload Examples

Example 1

onbeforeunload example 1
<body onbeforeunload="return message()">

    
        function message() {
            return "Are you sure you want to leave this page?";
        }
    

</body>

Example 2

onbeforeunload example 2
window.onbeforeunload = function message() {
            return "Are you sure you want to leave this page?";
        }

Example 3

onbeforeunload example 3
    window.addEventListener("beforeunload", function(event) {
        event.returnValue = "Are you sure you want to leave this page?";
    });

Browser Support

Chrome1
Edgeyes
Firefox1
Internet Explorer4
Opera12
Safari3
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android?
Opera Androidyes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: