检测浏览器是否导航到另一个页面

[英]Detecting if browser navigating to another page


I need to check if browser is navigating to another page or closing. My idea is;

我需要检查浏览器是否导航到另一个页面或关闭。我的想法是;

  1. Create global variable var isNavigating = false;
  2. 创建全局变量var isNavigating = false;

  3. Bind click event to make isNavigating = true of every anchor which will navigate to another page
  4. 绑定点击事件使每个锚点的isNavigating = true将导航到另一个页面

  5. Check if isNavigating true on body's unload event.
  6. 检查body的卸载事件是否为isNavigating为true。

Any other ideas? Recommendations?

还有其他想法吗?建议?

2 个解决方案

#1


8  

You can do this by the following script.

您可以通过以下脚本执行此操作。

<script type="text/javascript"> 
     window.onbeforeunload = function(){ return;} 
</script> 

However if you plan to cancel the navigaion, just don't bother. It's not possible as far as i know.

但是,如果您打算取消导航,请不要打扰。据我所知,这是不可能的。


Code below checks if the user has clicked a link.

下面的代码检查用户是否点击了链接。

var checkAnchorClick = false;

$(document).ready(function () {
    $("a").live("click", function () {
        checkAnchorClick = true;
    });
});

$(window).unload(function () {
    if (checkAnchorClick)
        alert("User clicked a link...");
    else
        alert("Something else...");
});

#2


0  

Just bind to onunload event of the window.

只需绑定到窗口的onunload事件。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/07/07/7202591b560d611d814a3ab519617871.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告