tags:

views:

109

answers:

6

I want to know how can I display a confirmation box when the users try to get out of the page, like this one here in StackOverflow when you try to close the "Ask Question" page.

Thanks you in advance.

A: 

That's browser based.

As long as you implement <form> tag, and you type in something in the form, and in Google Chrome, it will prompt you that message.

Ngu Soon Hui
I suspect you misunderstood the question... I'd remove this answer before it costs you a bunch of points :)
Dr.Dredel
+1  A: 

You want to catch the onbeforeunload event of window. Then if you return a string, the browser will display your message in a prompt. Here's an article with sample code.

Kevin Conner
+3  A: 

In javascript, attach a function to window.onbeforeunload, like so:

window.unbeforeunload = function (e)
{
    // return (prompt ('Are you sure you want to exit?'));

    // EDIT: Amended version below: 
    var e = e || window.event;
    if (e) e.returnValue = 'Your exit prompt';
    return 'Your exit prompt';
}

EDIT: As the comments below indicate, I had misunderstood the working of the event. It should now work as intended.

Ref: window.onbeforeunload (MDC)

K Prime
-1 Doesn't work in my tests. No prompt.
Eric Mickelsen
This will not work. You need to remove "prompt" and just return the string: `return 'You will lose any unsaved changes.';` It will surround the text you return with something like "Are you sure you want to navigate away from this page?" and then your text, and then, "Press OK to continue, or Cancel to stay on the current page."
Renesis
Renesis is right - that's the real problem with this script. It's a misunderstanding of the purpose of this function.
Eric Mickelsen
It should be noted that while IE adds the surrounding text, not all browsers do.
Eric Mickelsen
@tehMick amended my answer accordingly
K Prime
Results after testing -- I pulled the surrounding text above from Firefox, and Safari shows the basically the same text. Chrome showed no surrounding text.
Renesis
@K Prime -- what is the purpose of e.returnValue? I'm not sure I'm aware of any browsers that won't work with just the standard return value.
Renesis
K Prime
+1  A: 

You can use the window.onbeforeunload event to catch this. If there is any value inside the textarea then an alert message is shown.

rahul
+2  A: 

probably a dupe: http://stackoverflow.com/questions/821011/how-do-you-prevent-javascript-page-from-navigating-away, but you can do this by adding an delegate to the window.onbeforeunload event

<script type="text/javascript">
   window.onbeforeunload = function() {
      //will show a dialog asking the user if 
      //they want to navigate away from the current page
      // ok will cause the navigation to continue, 
      // cancel will cause the user to remain on the current page
      return "Use this text to direct the user to take action.";
   }
</script>

Update: doh! updated code to do what the OP really wanted :D

Jared
-1 returning false where you indicate in your comment actually brings up a dialog that says "false";
Eric Mickelsen
Now it's good. :-) Un-1.
Eric Mickelsen
lol yeah, would be nice if i caught that earlier, thanks!
Jared
+4  A: 

Actually, all that is necessary is this:

window.onbeforeunload = function(){ return "You should save your stuff."; }

This is also kind of a dupe of this: http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-cha

Eric Mickelsen