开发者

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 confir开发者_Go百科m to execute an ajax script and then to close or change the window. I don't know how to do that. PS: I'm using jQuery.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜