Javascript alert when user closes the tab ot the window

前端 未结 2 1479
灰色年华
灰色年华 2021-01-22 09:34

I want when a user closes the tab or window or when he tries to move to another location different from my site to pops a confirm box, and if he confirm to execute an ajax scrip

相关标签:
2条回答
  • 2021-01-22 10:19
    $(window).unload(function() {
        var answer=confirm("Are you sure you want to leave?");
    if(answer){
        //ajax call here
        }
    });
    

    Just add your own alert/dialogue code to the function.

    0 讨论(0)
  • 2021-01-22 10:29
    <script language="JavaScript">
      function unload() {
          alert('window closed');
      }
    window.onunload = unload;
    </script>
    
    0 讨论(0)
提交回复
热议问题