10

I'm using window.onbeforeunload to pop up a confirm dialog when a close event occurs, but the confirm dialog appears on page refresh and doesn't execute on page close.

Here's the JavaScript code:

<script language="JavaScript">
    window.onbeforeunload = confirmWinClose();
    function confirmWinClose() {
        var confirmClose = confirm('Close?');
        return confirmClose;
    }    
</script>

I tried it on Chrome, Firefox and Internet Explorer.

Adam Jensen
  • 533
  • 1
  • 10
  • 25
Java Player
  • 6,050
  • 26
  • 99
  • 154
  • 2
    just the concern maybe setting window.onbeforeunload = confirmWinClose(); this causing issue instead set it window.onbeforeunload = confirmWinClose; – Neha Dec 23 '13 at 09:52
  • ok, that prevents the code from being executed on page refresh but it also still doesn't executed on page close!! – Java Player Dec 23 '13 at 09:58
  • You *cannot* use a `confirm` inside `onbeforeunload`. That's not how it works. You need to have `onbeforeunload` return a string. That string will be displayed by the browser on its own confirm box. `onbeforeunload` is different than "normal" events. – Rocket Hazmat Jan 14 '14 at 15:27
  • You can actually do it either way. A confirm dialog will simply use the text inside confirm... confirm('[this text]'). Just like returning a string. They will do the same thing. – Bryce Nov 30 '16 at 16:53

4 Answers4

7

PROBLEM WITH YOUR CODE:

the function will be called when you refresh, because on refresh the page is unloaded and then reloaded.

in your solution, you should also note that you are not assigning a function to window.onbeforeunload but you are assigning the return value of the function when you write

window.onbeforeunload = confirmWinClose();

which might also execute the function (based on where you place it in the javascript) whenever the assignment is done. For e.g.

function confirmWinClose() {
var confirmClose = confirm('Close?');
return confirmClose;
}

window.onbeforeunload = confirmWinClose();

the above code will execute the confirmWinClose function whenever this js is loaded.

(not your case as you have defined the function after call, so won't be executed on load, but you should remember this)

SOLUTION:

the below solution is working for close also

instead of your solution, i tried this

JS:

window.onbeforeunload = function() {
    var confirmClose = confirm('Close?');
    return confirmClose;
}

or

 window.onbeforeunload = confirmWinClose; //note the absence of calling parantheses

    function confirmWinClose() {
        var confirmClose = confirm('Close?');
        return confirmClose;
    }

this works as expected.

also note that you should return from the onbeforeunload explicitly.

even if you have to call a function, you should do

<script>
    window.onbeforeunload = function(e) {
       callSomeFunction();
       return null;
    };
</script>
gaurav5430
  • 9,899
  • 5
  • 32
  • 73
3

No full solution, but you can intercept the F5 key (not intercepted if the user click on the refresh browser button...)

var isRefresh = false;

// with jquery

$(function() {
  $(document).on("keydown", function(e) {
    if (e.which === 116)
    {
      isRefresh = true;
    }
  });
});

window.onbeforeunload = function() {
 if (! isRefresh)
 {
   return confirm ('Close ?');
 }
 return false;
};
Paul Rad
  • 4,634
  • 1
  • 19
  • 20
1

Try this it will work. You were assigning the method to onload that need to execute when it event occur so its need to be like object. refer link for better explanation - var functionName = function() {} vs function functionName() {}

 window.onbeforeunload = confirmWinClose;
 function confirmWinClose () {
 var confirmClose = confirm('Close?');
 return confirmClose;
 };
Community
  • 1
  • 1
Neha
  • 1,532
  • 1
  • 10
  • 13
0

Since you anyway only want to display your text, What just using the onbeforeunload as it is expected just return the string?

<script language="JavaScript">
  window.onbeforeunload = confirmWinClose;
  function confirmWinClose() {return "close?";}
</script>
Daniel
  • 61
  • 9