-3

I have implemented the suggestion from

window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

but it still does not work

 var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; 

        myEvent(chkevent, function (e) { 
           
            var confirmationMessage = 'Are you sure to leave the page?';
            (e || window.event).returnValue = confirmationMessage;
            return confirmationMessage;
        });

I have tried this but it is not working in mozilla. it works for once only. please help me to solve this.

I have solved this from testing process. there was lacking of user interaction in firefox. If user interact with page after that user confirm message appear.

Community
  • 1
  • 1
Dhrutika Rathod
  • 540
  • 2
  • 6
  • 22
  • Since you likely only have one onbeforeonload on the page, try to see if this works better: `window.onbeforeunload=function () { var confirmationMessage = 'Are you sure to leave the page?'; window.event.returnValue = confirmationMessage; return confirmationMessage; }` - Are there any messages in the console? – mplungjan Nov 07 '16 at 06:52
  • Sorry @mplungjan. but it is not working in firefox. – Dhrutika Rathod Nov 07 '16 at 06:57
  • 1
    What does that mean? Errors? Console messages? – mplungjan Nov 07 '16 at 06:58
  • There is no any error message. Here i want to prompt before closing window. so i click on close button it just close. it is not giving any error message – Dhrutika Rathod Nov 07 '16 at 07:03
  • Did you, the user. interact with the page (e.g. enter some text, or something into an ``)? Firefox does not show this message unless the user has interacted with the page (i.e. the user will loose data they entered into the page). – Makyen Nov 07 '16 at 07:11
  • In `about:config` what are the values for `dom.disable_beforeunload` and `dom.require_user_interaction_for_beforeunload`? – Makyen Nov 07 '16 at 07:15
  • The code works as written in Firefox 49.0.2 when copy&pasted into the console. – Makyen Nov 07 '16 at 07:42
  • @Makyen thank you this is work with user interaction as you suggest in you comment – Dhrutika Rathod Nov 07 '16 at 08:20
  • 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 Nov 07 '16 at 11:16
  • Check my answer there, it covers your options with up-to-date browsers and they support on each of them. – Dekel Nov 07 '16 at 11:16
  • @Dekel, While your answer to that question provides good information (and you do describe the user interaction which is required in Firefox), this question is not a duplicate of that one. The actual questions are different. Just closing this one with that as a dup target does not leave the later reader with the knowledge that the code here works, but that there was a failure in testing. While having a question/answer which indicates that user interaction is required may be useful, this question is worded such that the issue is a failure in testing method, which makes it not reproducible. – Makyen Nov 07 '16 at 20:49
  • A duplicate is not always an exact duplicate. Sometimes it's enough to be close-enough one, or even kind of "not the same answer, but the question gives all the information you need". I'm pretty sure the answer there gives the information in this case. – Dekel Nov 07 '16 at 20:52
  • @Dekel this question is not a duplicate of you suggested. – Dhrutika Rathod Nov 08 '16 at 04:39
  • @DhrutiRathod, if you check my suggestion you will see that your code works great in firefox, however it **require** some interaction with the page, and new version of firefox will not give you the ability to control the text of the alert box (that is basically your question, and this is why I marked it as dup). – Dekel Nov 08 '16 at 12:11

1 Answers1

1

Firefox does not display beforeunload or unload custom messages for the sake of the security of the user.

Prior example of this question

Community
  • 1
  • 1
AM Douglas
  • 1,873
  • 1
  • 13
  • 17
  • This is inaccurate. Firefox does display such, if the user has interacted with the page (i.e. if the user will loose data when the page is unloaded). – Makyen Nov 07 '16 at 07:17
  • Sorry, I meant **custom** messages. – AM Douglas Nov 07 '16 at 07:23
  • Yes, custom messages were [removed in Firefox 44.0](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Browser_compatibility). – Makyen Nov 07 '16 at 07:25
  • sorry. but i do not get any message. my issue is beforeunload not working. please help me. – Dhrutika Rathod Nov 07 '16 at 07:27
  • 1
    You won't get the message because custom `beforeunload` messages were removed from Firefox. We literally just said this. – AM Douglas Nov 07 '16 at 07:28
  • @DhrutiRathod, then you are doing something wrong in your testing. Your code works as written when copy&pasted into the console to display the stock message, "This page is asking you to confirm that you want to leave - data you have entered may not be saved.", at the times Firefox displays it. – Makyen Nov 07 '16 at 07:44