1

now im doing laravel project. i want to implement javascript code only for preventing the back button and close tab window. i read the article that we can use onbeforeunload(). but this code not suitable with me. because the warning message will display when i want to go to another link menu on my site..

here is my code

window.onbeforeunload = function() {
   return "Do you really want to leave?";
};

above code still have problem to me :

  1. i must click at list 1 time on the side. then click close tab, so the pop up will display. if i dont click, the pop up not display and directly close the tab.
  2. im using opera browser but the custom message not display same as what i type. it display "Changes you made may not be saved.". How do i could display the sentence as same as i type?

is there any other code that just prevent only for back button and close the tab? so went i want to navigate to other link menu on my site, the warning message not display.

please help

N69S
  • 16,110
  • 3
  • 22
  • 36
ferdinand
  • 325
  • 2
  • 14
  • 1
    JavaScript in page **cannot** prevent users from closing the tab or window or going back - that's by design, otherwise think of the amount of online advertising we'd be forced to sit through. – Dai Aug 07 '20 at 12:03
  • @Dai there is a mechanism for something like it; try typing in the answer box and closing the tab, and your browser should ask you if you really want to close the tab. – Haem Aug 07 '20 at 12:12
  • Does this answer your question? [How to control browser confirmation dialog on leaving page?](https://stackoverflow.com/questions/19179514/how-to-control-browser-confirmation-dialog-on-leaving-page) – Haem Aug 07 '20 at 12:15
  • Antoher candidate: https://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own – Haem Aug 07 '20 at 12:17

1 Answers1

0
 //you should $event.return = "";
 window.addEventListener("beforeunload", function ($event) { 
      return $event.returnValue = "";
    })
hassan khademi
  • 1,156
  • 12
  • 14