jQuery卸载事件仅用于关闭窗口而不用于链接导航(jQuery unload event only for close window not for Link navigation)

我在关闭页面时使用此代码注销用户,但用户也会在点击其他链接(同一网站)时注销:

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

有没有办法区分链接导航和真实页面关闭?

编辑:

我目前正在实施此解决方案,但它缺乏检测页面重新加载

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

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

最满意答案

尝试以下解决方案,希望这会有所帮助

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

要么

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

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卸载事件仅用于关闭窗口而不用于链接导航(jQuery unload event only for close window not for Link navigation)

我在关闭页面时使用此代码注销用户,但用户也会在点击其他链接(同一网站)时注销:

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

有没有办法区分链接导航和真实页面关闭?

编辑:

我目前正在实施此解决方案,但它缺乏检测页面重新加载

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

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

最满意答案

尝试以下解决方案,希望这会有所帮助

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

要么

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

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