简体   繁体   中英

how to prevent window back button and close tab with javascript

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

 //you should $event.return = "";
 window.addEventListener("beforeunload", function ($event) { 
      return $event.returnValue = "";
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM