检测浏览器是否导航到另一个页面 [英] Detecting if browser navigating to another page

查看:63
本文介绍了检测浏览器是否导航到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 创建全局变量 var isNavigating = false;
  2. 绑定单击事件以使将导航到另一个页面的每个锚点的 isNavigating = true
  3. 检查正文的 unload 事件中 isNavigating 是否为真.
  1. Create global variable var isNavigating = false;
  2. Bind click event to make isNavigating = true of every anchor which will navigate to another page
  3. Check if isNavigating true on body's unload event.

还有其他想法吗?建议?

Any other ideas? Recommendations?

推荐答案

您可以通过以下脚本进行此操作.

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

这篇关于检测浏览器是否导航到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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