admin管理员组

文章数量:1394149

Trying to figure out how to make a modal pop up on browser window exit. I know how to do an alert box, but I want to have users answer a questionnaire on exit, instead of a basic "are you sure you want to leave" alert box.

Trying to figure out how to make a modal pop up on browser window exit. I know how to do an alert box, but I want to have users answer a questionnaire on exit, instead of a basic "are you sure you want to leave" alert box.

Share Improve this question asked Feb 14, 2012 at 17:05 THEDertTHEDert 592 silver badges10 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2
window.onbeforeunload = function () {
  return 'Your content has not been properly saved yet!';
};

This will make the browser display a confirmation box like this one:

As you can see, the string returned by the function is shown in the box.

You can't. The only way to stop the window from closing is to use the JS Confirm (or alert). If you were to just pop-up a modal in-page, the window would still close and you'd never see it. Your best bet is to open the JS alert, then redirect the page to the question. Though note that that is incredibly annoying for the user.

While it is not always remended to put code on exit, you could do something like this:

<body onUnload="javascript:openDialog()">

or

$(document).bind('unload', openDialog);

Then:

function openDialog(e) {
    e.preventDefault();
    $("#yourQuestionnaire").dialog('open');
}

But generally it is a good practice to avoid binding to the onUnload event because its firing isn't reliable in all browsers and situations.

UPDATE

The documentation shows it as:

$(window).unload(function() {
  alert('Handler for .unload() called.');
});

This works for me when applying it via the Firefox/Firebug console on any Stackoverflow page.

Documentation here: http://api.jquery./unload/

I had similar requirements where I wanted to use nice HTML message instead of default js message. After spending some time I finally found a way to do it. I have used jQuery Colorbox modal which is very useful in any modal requirement, not just here.

Basically you can use mousemove event and find if Y coordinate (e.clientY ) is less than 5. So it will be fired every time when mouse is near address bar (user typing new URL or trying to close window).

jQuery(document).ready(function() {
jQuery(document).mousemove(function(e) {
    if (e.clientY <= 5) {
        jQuery.colorbox({initialHeight: 250, initialWidth: 250, html:'<h2>any custom HTML here</h2><br/><img src="nice-img" />'});
    }
});

});

I found many example using e.pageY but note that it will only be fired if user has not scrolled down. So if you scroll down even for 5 pixel, e.pageY will not work, but e.clientY will work. e.pageY gives offset while e.clientY gives absolute coordinate. Very important!

Here is a simple light-weight JQuery plugin that you could refer for your scenario which tracks your mouse pointer and triggers the modal as soon as the mouse moves out of the viewport: jQuery Exit Modal Plugin Example

$(window).on("unload",function() {
   alert("Handler for .unload() called");
});

NOTE: .unload() has been deprecated in version 1.8 and removed in jQuery 3.0; please 
use .on( "unload", handler ) instead of .unload( handler ). 

See here : unload(handler)

本文标签: javascriptModal Popup on Window ExitStack Overflow