3

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.

Bernardo Amorim
  • 343
  • 1
  • 6

6 Answers6

6

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: How to show the "Are you sure you want to navigate away from this page?" when changes committed?

Community
  • 1
  • 1
Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
3

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
  • 5,809
  • 1
  • 25
  • 19
  • 1
    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." – Nicole Jan 19 '10 at 05:50
  • Renesis is right - that's the real problem with this script. It's a misunderstanding of the purpose of this function. – Eric Mickelsen Jan 19 '10 at 05:51
  • It should be noted that while IE adds the surrounding text, not all browsers do. – Eric Mickelsen Jan 19 '10 at 05:53
  • 1
    @tehMick & Renesis - you're right, I was mistaken; amended my answer accordingly – K Prime Jan 19 '10 at 06:22
  • Results after testing -- I pulled the surrounding text above from Firefox, and Safari shows the basically the same text. Chrome showed no surrounding text. – Nicole Jan 19 '10 at 06:26
  • @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. – Nicole Jan 19 '10 at 06:27
  • @Renesis - MDC recommends it for IE & FF, see the linked ref. FF accepts both, but due to no real standardisation, I'm not sure which is the 'correct' one. Chrome seems to work fine while displaying the exit prompt – K Prime Jan 19 '10 at 07:05
2

probably a dupe: How do you prevent a webpage from navigating away in JavaScript?, 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

Community
  • 1
  • 1
Jared
  • 8,390
  • 5
  • 38
  • 43
1

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.

easeout
  • 8,665
  • 5
  • 43
  • 51
1

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
  • 184,426
  • 49
  • 232
  • 263
-4

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.

Graviton
  • 81,782
  • 146
  • 424
  • 602