1

I am trying to call this confirmation function on onbeforeunload event but its not getting called. What am I missing here?

I am calling this inside the body tag:

<body onbeforeunload="confirmation();">

I also tried:

<body onbeforeunload="return confirmation();">

Code:

  function confirmation(){
  var answer = confirm('You have unsaved changes!!');
   if(answer == true)
    {
     alert('Bye Bye!!!');
    }
    else{
      alert('You are staying');
     return false;
    }
   }
4
  • Can you please clarify either your sample code or your title to reflect which event you are trying to capture? Commented Jun 2, 2011 at 18:12
  • 1
    Where's the onbeforeunload? :) Commented Jun 2, 2011 at 18:12
  • 1
    That's not how a "beforeunload" handler works. Read this.. As it is now, regardless of the answer to your "confirm()" call, the page will be reloaded. Commented Jun 2, 2011 at 18:14
  • Hi tweeZz, I didnt post it. it's just <body onbeforeunload="confirmation()"> Commented Jun 2, 2011 at 18:15

2 Answers 2

5

That's not remotely how onbeforeunload works, and the code contains syntax errors.

window.onbeforeunload = function (e)
{
    var e = e || window.event,
        message = 'You have unsaved changes!!';

    // For IE and Firefox prior to version 4
    if (e)
    {
        e.returnValue = message;
    }

    return message;
}
Sign up to request clarification or add additional context in comments.

15 Comments

And note that the string that's returned may or may not be shown to the user. (In newer browsers it isn't.)
@t0mcat are you returning a string from the function, and are you setting the "returnValue" property of the event object?
@t0mcat all you need is the code that is in Matt Ball's answer. That's it.
You should add it in a <script> tag, and you don't need anything at all on the <body> tag.
There's a discussing regarding this problem here stackoverflow.com/questions/276660/… . I think its not possible :(
|
3

Soooo.. You're trying to ask the user for confirmation when he made changes on your page that will not be saved if he leaves the page. Is that correct?

If so, then that seems to be possible. Try the following on SO. Start to write an answer on this question and then try to close the browser window.

I tested in Chrome 12, FF 4.0.1 and IE 9.
All 3 browsers show a popup window. It seems they all contain a custom text. Chrome shows the cleanest message, meaning a popup with only the custom message and 2 buttons.
In all 3 browsers trying to close the browser in any normal way triggers a popup that allows the user to choose if he maybe doesn't rather stay on the page and finish what he started. If he does want to leave, he can. (killing the browser process or a computer shutdown might not do what you would like thoug :) ).

Internet Explorer:
enter image description here

Firefox:
enter image description here

Chrome:
enter image description here

I would try the suggestion of hooking up a function that returns a string to the window.onbeforeunload event. Try this:

<script>
window.onbeforeunload = function (evt) {
  var message = 'You have started writing or editing a post.';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.