2

Solved in Is it possible to display a custom message in the beforeunload popup?

Original question

I'm curently using Firefox ESR 45.3.0, and I've written a small test page:

<html>
    <body>
        <script type="text/javascript">
            window.onbeforeunload = function() {return "foo"};
        </script>
        <p>content</p>
    </body>
</html>

erroneous behavior:

  1. load any page
  2. load this test page (can also be reproduced via "file:///...")
  3. click browser back
  4. the confirm dialog is not shown

correct behavior:

  1. load any page
  2. load this test page
  3. click inside the window
  4. click browser back
  5. the confirm dialog is shown

I've also played around with the console (developer tools), trying to set focus on window or body, still not working without clicking the page. My original problem page is longer, instead of 7. you can also scroll the page by mousewheel, this also "activates" the onbeforeunload handler.

Strange, looks like a Firefox bug, do you have any hints how to deal with this problem?

Regards, Gunnar

Update 1

Working with e.returnValue does not fix the problem, I've tested it in the test page shown above.

The test page is very simple, my original javascript code in the original problem page is copied from some other question/answer and goes like

function checkModelChanged(modelChanged, confirmText)
{
    if (modelChanged === true)
    {
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable

        myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
            (e || window.event).returnValue = confirmText;
            return confirmText;
        });        
    }
}

Update 2

  1. The test page works as expected in Chrome 52.0.2743.116 m and Edge 38.14393.0.0 (EdgeHTML 14.14393). Browser back without "window interaction" shows the confirm dialog.

  2. The original problem page is part of a Liferay Portlet (JSF 2.2 and Primefaces 5.3), and I've already tried javascript's scollIntoView and jQuery's animate (scrollTop) or trigger (click).

Community
  • 1
  • 1
Gunnar
  • 383
  • 4
  • 18
  • 1
    Possible duplicate of [Is it possible to display a custom message in the beforeunload popup?](http://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup) – Dekel Sep 01 '16 at 11:53
  • 2
    no it isn't a duplicate of `is it possible to display a custom message` - because it's not a custom message that the question is asking for – Jaromanda X Sep 01 '16 at 11:57
  • `Update 2 point 1` - firefox **requires** interaction before this event will fire - which makes sense, without interaction, there can be no data to lose – Jaromanda X Sep 01 '16 at 12:01
  • @JaromandaX, this is a duplicated because it says exactly which browsers **accept** a custom message and which don't, and also regarding update #2 - It also there in the answer of the duplicated question. – Dekel Sep 01 '16 at 12:28
  • @Dekel - I shoulda clicked da link shouldn't I :p – Jaromanda X Sep 01 '16 at 12:29
  • @JaromandaX, what? – Dekel Sep 01 '16 at 12:33
  • @Dekel - you're right, it is a duplicate - I was posting "dumb" version of "I should have clicked the link of the duplicate rather than just read the question in th elink text, then I would've seen that all the information was right there" :p – Jaromanda X Sep 01 '16 at 12:34
  • @JaromandaX oh :) well, you are welcome to upvote my answer there ;) – Dekel Sep 01 '16 at 12:34
  • well, no wonder you knew about it :p – Jaromanda X Sep 01 '16 at 12:35
  • @JaromandaX, it took some time to validate and check all browsers and stuff... – Dekel Sep 01 '16 at 12:36
  • Thanks Dekel, upvoted your answer over there. – Gunnar Sep 01 '16 at 12:46
  • Sure :) you are welcome! Glad I could help. – Dekel Sep 01 '16 at 13:36
  • The problem is not only with the "back" button. It also appears when you try to close the tab and you didn't interacted withe the page first. FF62. I think the problem is for every trigger of "onbeforeunload". @JaromandaX "firefox requires interaction before this event will fire - which makes sense" - what about if you are loading many initialisation data in an offline-capable web-app and you want to avoid an unintended tab-close, in order not to lose this data or the static content? I thin it would make sense to see the dialog without having interacted with the page. – NicDD4711 Jun 23 '20 at 08:36
  • The question is not how to fix a FF-bug wich is no bug. The question is how to programmatically do possible in javascript, that "beforeunload" triggers without initially user interaction. About the sense, I explained above. With this title, this question is not a duplicate. – NicDD4711 Jun 23 '20 at 09:22
  • @user3408530 - see https://stackoverflow.com/questions/38879742/is-it-possible-to-display-a-custom-message-in-the-beforeunload-popup – Jaromanda X Jun 23 '20 at 13:25

0 Answers0