0

The closing of a window in JavaScript is technically quite easy for a Desktop Browser environment. However, with Mobile Browsers suppressing different properties and methods, closing windows in Mobile Browsers become a nightmare.

Alert messages can be used to notify the user about something special: danger, success, information or warning.

So our question is as follows:

How to show an alert message when closing a Mobile Browser window?

Francisco Maria Calisto
  • 2,841
  • 4
  • 22
  • 54

1 Answers1

2

If you are looking into connecting "user friendlyness" with this, that is not the way to go. In spite of, you might look into JQuery's website, using Jquery Detect unload or close.

The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behaviour should be tested on all supported browsers, and contrasted with the proprietary before unloading event.

SOLUTION 1

$(window).on('beforeunload ',function() {
    return 'Are you sure ?';
});

SOLUTION 2

<script>
window.onbeforeunload = function (e) {
    e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = 'Sure?';
    }

    // For Safari
    return 'Sure?';
};
</script>

Again, look at the whole question on the userfriendlyness side.

BDCardoso
  • 36
  • 2