仅用于链接导航关闭窗口不jQuery的卸载事件关闭窗口、事件、链接、jQuery

2023-09-11 00:59:44 作者:似有若无

我用这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>
 
jquery可滑动变长导航菜单

  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});   
    }
});