0

I would like to make an alert, that when someine will refresh the page it will ask him or inform him something.

There are some examples, but in any case it works properly for me.

For instance, for code I've used here:

   window.onbeforeunload = function() {
   return "Data will be lost if you leave the page, are you sure?";
   };  
   $(document).on("submit", "form", function(event){
   window.onbeforeunload = null;
   });

I have constant message, like below:

enter image description here

The same applies to the second code:

 window.addEventListener('beforeunload', (event) => {
   event.returnValue = `Are you sure you want to leave?`;
 });

Is it something I am doing wrong here? I don't understand why the alert message is still thhe same.

Geographos
  • 827
  • 2
  • 23
  • 57
  • 1
    Related/duplicate : https://stackoverflow.com/a/37782307/16688813 – Tom Nov 08 '21 at 16:08
  • For the `beforeunload` popup, all major browsers don't support custom messages. They have started ignoring the actual message and just showing their own message. Even you can not set/put `confirm` or `alert` inside `beforeunload`. This issue is already discussed in many blogs and posts. – Ruhul Amin Nov 08 '21 at 16:28

1 Answers1

0

Browsers now do not allow the app to specify that string any more. It was used maliciously to jam spam in.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445