最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript alert when user closes the tab ot the window - Stack Overflow

programmeradmin1浏览0评论

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 and then to close or change the window. I don't know how to do that. PS: I'm using jQuery.

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 and then to close or change the window. I don't know how to do that. PS: I'm using jQuery.

Share Improve this question asked Jun 8, 2011 at 14:18 NikolayNikolay 1652 silver badges14 bronze badges 2
  • 2 Please stop asking duplicate questions. If you need to revise your question, simply edit it. – user50049 Commented Jun 8, 2011 at 14:53
  • 1 Also, would it kill you to accept some answers to your questions? 14% is a dismal acceptance rating no matter the reason. – Chris Commented Jun 8, 2011 at 15:08
Add a ment  | 

2 Answers 2

Reset to default 5
$(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)

  1. 暂无评论