5

There is an old 2005 bug in the Firefox based browsers that makes the confirmation dialog show up twice when closing a window which had some data entered.

The "beforeunload" event happens twice, the second time can be ignored.

There are questions/answers on stackoverflow.com about that and the fix is to add a flag that you set to false, so the second attempt is ignored. A timer is used to reset the flag in case the user decides to stay on the page.

There is a copy of my own implementation in my Snap! editor:

snapwebsites.Editor.prototype.initUnload_ = function()
{
    var that = this;
    jQuery(window).bind("beforeunload", function()
        {
            return that.unload_();
        });
};

snapwebsites.Editor.prototype.unload_ = function()
{
    var key,                // loop index
        that = this;        // this pointer in the closure function

    if(!this.unloadCalled_)
    {
        for(key in this.editorForms_)
        {
            if(this.editorForms_.hasOwnProperty(key))
            {
                if(this.editorForms_[key].toBeSaved()
                && this.editorForms_[key].wasModified(true))
                {
                    // add this flag and timeout to avoid a double
                    // "are you sure?" under Firefox browsers
                    this.unloadCalled_ = true;

                    // we create a function in a for() loop because
                    // right after that statement we return
                    setTimeout(function(){
                            that.unloadCalled_ = false;
                        }, 20);

                    // TODO: translation
                    //       (although it doesn't show up in Firefox based
                    //       browsers many others do show this message)
                    return "You made changes to this page! Click Cancel to avoid closing the window and Save your changes first.";
                }
            }
        }
    }

    return;
};

(For more of that source code, it is available in git on SourceForget.net: https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/plugins/editor/)

As you can see I use a flag, this.unloadCalled_, which I set to true when I first return. I reset the flag in the timeout function. The 20 in there could even be zero (0). Just to be safe I kept a larger number, but it worked for me with zero.

The reason for the double request is because Firefox considered the closure of the window and the one of the tab (something like that, I read that in a bug report on mozilla.com).

  • Another workaround might be not to use old firefox ;-) – John Dvorak May 25 '14 at 9:31
  • 1
    @JanDvorak: according to the linked bug report this bug is still present in FF 26, which is an officially supported FF version. – Martijn Pieters May 25 '14 at 9:50
  • 1
    Reproduced in Firefox 29 on Mac OS X, when writing an answer while not having any other tab open, and using Cmd-W to close the window, and selecting "Leave page" for the "This page is asking you to confirm that you want to leave - data you have entered may not be saved." (When selecting "Stay on page" in the second confirmation, I cannot even close any tab at some later time, but need to quit Firefox.) – Arjan May 25 '14 at 10:26
  • (There are more situations in which I can reproduce this in Firefox 29; added to the Bugzilla report; cc. @Martijn) – Arjan May 25 '14 at 11:01
  • So... does that mean you'll wait on Firefox to fix it or you're going to apply a fix like mine to Stackoverflow until then? – Alexis Wilke Jun 1 '14 at 7:39
  • @JanDvorak, what do you mean? This happens in the newest FF and SeaMonkey (which I use). If it does not happen for you, maybe I'm one version behind, but I'd be surprised. Reading the bugzilla report it was presented as a "normal behavior". – Alexis Wilke Jun 1 '14 at 7:41

You must log in to answer this question.

Browse other questions tagged .