views:

54

answers:

4

I've used window.onbeforeunload to display a custom message when a user attempts to leave a site.

Example:

window.onbeforeunload = function(){
  if(some_condition){
    return "Are you sure you want to navigate away from this page?\nAll unsaved changes will be lost.";
  }
};


+--------------------------------------------------------+
| Are you sure you want to navigate away from this page? |
| All unsaved changes will be lost.                      |
|                                                        |
|          [ Yes ]  [ Cancel ]                           |
+--------------------------------------------------------+

However, I'd like to enhance this a bit. If possible, I'd like to use a custom modal form instead of the generic popup.

Is there a way to do this?

A: 

Is there a way to do this?

Nope.

You are stuck with the prompt the browser gives you.

epascarello
Source? Explanation?
macek
-1 there are at least some partial ways to do this.
Moin Zaman
@Moin, what you posted is hijacking all of the click events on the page. Use the refresh button, back button, close the browser. It will fail.
epascarello
@macek, There is no source. It is known fact. You can not change alerts, confirms, prompts, or any other windowed element. You can not change the onbeforeunload dialog. These windows are standard for a reason, so people know what they are getting. If you could prevent someone from leaving with your idea, a hacker would love it. Just like the pop up windows in the old days where you could hide the close button. Some people had good intentions with there being no close, but advertisers had "good" intentions too.
epascarello
epascarello, I'm voting this down because this is not a useful answer if you can't back it. Saying it is a "known fact" doesn't help. By the way, you *can* change the content of the onbeforeunload dialog. See my code example above. Because there was some leeway here, I was wondering if there was more.
macek
@macek, There is nothing to back it up. Documentation on onbeforeunload is basically non existant since it is a MS made up thing that other browsers copied. Look at the docs https://developer.mozilla.org/en/DOM/window.onbeforeunload and you will see they give zippo information on it. You can add a message to the dialog, BUT you can not change the look and feel which is what you are implying. Changing the text is hardly leeway since you can do that with all of the other windowed elements [alert, confirm, prompt]. You are hoping for a miracle of an answer and you will not get one.
epascarello
+1  A: 

The unload event will fire when a user tries to navigate away. However, if you use a DIV as a pop-up the browser will navigate away before the user has a chance to read it.

To keep them there you'd have to use a alert/prompt/confirm dialog boxes. (as far as I know)

John Strickler
Hmm, that's what I was worried about. Thanks anyway.
macek
A: 

have a look at this: http://rewdy.com/tools/leavenotice-jquery-plugin

From the source you should be able to hack something together. http://github.com/rewdy/leaveNotice/blob/master/uncompressed/jquery.leaveNotice.js

also worth reading: http://stackoverflow.com/questions/944234/how-to-popup-when-a-visitor-exits-from-a-website

Moin Zaman
Fails with browser refresh, back button, forward button, close button, etc. Too many fails to be a good solution.
epascarello
@Moin Zaman, this is a nice little tool, but I find it wouldn't be useful for what I'm trying to do. It obviously only works with elements within your page. The browser's buttons (back, forward, refresh, close, home, etc) would not be intercepted here.
macek
+1  A: 

If they click the back button or something similar, I believe the alert/prompt/confirm boxes are your only option.

However, you can probably listen for specific keypress events, like ctrl/cmd + w/r, and interrupt those with a dialog.

hookedonwinter