I want to show a message whenever you are leaving the page (not an annoying alert, just some html telling you to wait), in thinking about it I'm facing certain difficulties:
when the user presses Stop in the browser, cancelling the navigate-away action, I'd like the message to go away.
whenever any link is clicked, the message should appear.
it shouldn't capture when the clicked link just opens another tab ( ignore _blank target )
that being said, firing the event is pretty simple, with just something like
$(document).unload(function()
{
// display message
});
the problem being that if the user cancels, the message wouldn't go away.
a possible fix would be:
$(window).unload(function()
{
// display message
setTimeout(function()
{
// hide message
},5000);
});
but I wanted to know if there was a cleaner way, that just when the user cancels the navigation (or it fails for any other reason), I can hide the message.
Edit #2:
I just noticed that with the above code, in FF the message isn't displayed until the page is left, at which point if the user presses Stop, he will receive about:blank. If he presses Stop before that, then the message is never displayed. Which is exactly what I wanted.
In internet explorer the message is never displayed, I'm assuming that's because IE handles stuff differently. I wonder what happens in chrome?