仅用于链接导航关闭窗口不jQuery的卸载事件 [英] jQuery unload event only for close window not for Link navigation

查看:141
本文介绍了仅用于链接导航关闭窗口不jQuery的卸载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code何时关闭页面注销用户,但用户也将注销上的其他链接(同一网站)点击时:

  $(窗口).unload(函数(){
    $阿贾克斯(网址:{url:;,异步退出与放离开= Y​​ES?:虚假})
  });
 

有没有什么办法的链接导航和真正的网页接近区别开来?

编辑:

我目前实施这一解决方案,但它缺乏检测页面重载

  $('A')。点击(函数(){
      VAR URL = $(本).attr(HREF);
      window.onbeforeunload = NULL;
      $(窗口).unbind('beforeunload');
      了window.location = URL;
  });
 

解决方案

尝试以下解决方案,希望这有助于

 <脚本>
$(窗口).bind('点击',函数(事件){
    如果(event.target.href)
        $(窗口).unbind('beforeunload');
});
$(窗口).bind('beforeunload',函数(事件){
    $阿贾克斯(网址:{url:;,异步退出与放离开= Y​​ES?:虚假});
});
< / SCRIPT>
 

  VAR logOutFlag = TRUE;
$('A')。点击(函数(){
    logOutFlag = FALSE;
});
$(窗口).bind('beforeunload',函数(事件){
    如果(logOutFlag){
         $阿贾克斯(网址:{url:;,异步退出与放离开= Y​​ES?:虚假});
    }
});
 

I am using this code for logging out user when closes the page, but the user will logout also when clicking on other links (same website):

  $( window ).unload(function() {
    $.ajax({url:"?logout&leave=yes", async:false})
  });

Is there any way to distinguish between link navigation and real page close?

EDIT:

I am currently implemented this solution, but it lacks to detect page reload

  $('a').click(function(){
      var url = $(this).attr("href");
      window.onbeforeunload = null;
      $(window).unbind('beforeunload');
      window.location = url;
  });

解决方案

Try the following solutions, hope this helps

<script>
$(window).bind('click', function(event) {
    if(event.target.href) 
        $(window).unbind('beforeunload');
});
$(window).bind('beforeunload', function(event) {
    $.ajax({url:"?logout&leave=yes", async:false});
});
</script>

or

var logOutFlag = true;
$('a').click(function(){
    logOutFlag = false;
});
$(window).bind('beforeunload', function(event) {
    if(logOutFlag){
         $.ajax({url:"?logout&leave=yes", async:false});   
    }
});

这篇关于仅用于链接导航关闭窗口不jQuery的卸载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆