-1

Currently, I'm using this code:-

window.addEventListener('beforeunload', (event) => { 
        event.preventDefault();
        event.returnValue = '';
});

enter image description here

But it does not appear what I want. What else JavaScript code available?

enter image description here

Barbora
  • 921
  • 1
  • 6
  • 11
  • see [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) – Jaromanda X Jun 18 '21 at 04:43
  • Does this answer your question? [JavaScript before leaving the page](https://stackoverflow.com/questions/7080269/javascript-before-leaving-the-page) – Sanmeet Jun 18 '21 at 04:53
  • Please follow the prompt and replace `enter image description here` with what to expect when following the hyperlink. – greybeard Jun 18 '21 at 05:11
  • Here you go: https://stackoverflow.com/questions/11835217/is-there-a-callback-for-cancelling-window-onbeforeunload – Kuldeep Singh Jun 18 '21 at 05:33

1 Answers1

0
window.addEventListener('beforeunload', (event) => { 
        event.preventDefault();
        event.returnValue = '';
});

Will not do anything as preventDefault() cannot stop page from loading. Instead you should use return false;. Also with that nothing else can come in the function and the user can just choose whether to stay or leave. So your code will become:

window.addEventListener('beforeunload', (event) => { 
    return false;
});

If you add anything else in the function your code will not work.

Tejas Gupta
  • 496
  • 2
  • 8
  • Thanks to help me. Actually, I want to enable this function when leaving the page, but it appears different from what I want. I want the functions appear this link https://i.stack.imgur.com/LCyfg.jpg – Zamri Abu Bakar Jun 18 '21 at 06:47