Skip to content Skip to sidebar Skip to footer

Javascript Alert When User Closes The Tab Ot The Window

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 script a

Solution 1:

$(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.


Solution 2:

<script language="JavaScript">
  function unload() {
      alert('window closed');
  }
window.onunload = unload;
</script>

Post a Comment for "Javascript Alert When User Closes The Tab Ot The Window"