Search code examples
javascriptfirefoxbrowserconfirm

Unexpected behaviour of onbeforeunload handler/function in firefox 32.0.3 and IE


I am trying to show a confirmation box when the user leaves the page and then use its returned value in onbeforeunload handler/function like

window.onbeforeunload = closingCode;
   function closingCode(){
   return confirm("You are leaving the page without saving some data. Do you want to continue?");
}

When this function is executed in the mentioned version of firefox, The window gets directly closed or refreshed without showing any confirmation dialog. I have tried to debug the script with firebug which shows that this function executes.

But when I use the code written below, A built-in browser confirmation dialog appears with a generic message.

window.onbeforeunload = closingCode;
       function closingCode(){
       return false;
    }

In Internet Explorer 8, the first code snippet results in two confirmation dialogs. One is mine and the other one is browser's default. whereas the the second snippet behaves the same.

I've tested this functionality here in stackoverflow, and found that It also shows a built-in confirmation dialog with the same generic message.

Is it possible to show a window.confirm() dialog, with desired message, in onbeforeunload handler/function or not? If yes, then kindly tell me how. Else, tell me why its not possible and provide some related links.


Solution

  • The onbeforeunload handler is supposed to return a string. The browser will then display this message in a prompt.

    function closingCode () {
        return "You are leaving the page without saving some data. Do you want to continue?";
    }