views:

74

answers:

2

When a user clicks on a button or link, I use the SimpleModal jQuery plugin to display a dialog to overlay the entire page, preventing the user from clicking another button or link during the delay before the next page loads. (I'd like to avoid this, but that's an issue for another day.)

After the next page displays, if the user clicks the Back button, the previous page still has the SimpleModal overlay displayed, preventing them from using the page. This is a problem.

How can I cause the SimpleModal dialog to close automatically either when the leaves the page or when the user clicks the Back button to return to the page?

I tried this without success:

$("body").unload(function() {
    $.modal.close();
});

Thanks!
Wally

Update: Here's the solution that seems to work the best:

$(window).bind("beforeunload", function() {
    $.modal.close();
});
A: 

Why not just

$.modal.close();

in your $(document).ready()? It shouldn't already be open if it's the user's first time to the page, and if it is, it will be closed automatically. If you have a certain condition under which you want the modal to be open when the person loads the page, just set that by doing a conditional if{} check.

dclowd9901
I tried that, but that didn't work either. As best as I can tell, when the user clicks the Back button, the previous page is displayed, but no events are triggered.
Wally Hartshorn
+1  A: 

Try using:

$(window).unload(function() {
   $.modal.close();
});
ryanulit
That works! Thanks!Actually, I found another solution that works slightly better. For some reason, using $(window).unload() results in a slight delay between the time when the modal overlay is closed and the time when the page unloads and the next page is displayed. It's just a fraction of a second, but it's a little annoying.Using onbeforeunload instead seems to eliminate that delay. Here's the code: $(window).bind("beforeunload", function() { $.modal.close(); });Apparently jQuery doesn't have a beforeunload function, so you have to explicitly bind it to the window.
Wally Hartshorn