检测浏览器是否导航到另一个页面 [英] Detecting if browser navigating to another page
本文介绍了检测浏览器是否导航到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查浏览器是否正在导航到另一个页面或正在关闭.我的想法是;
I need to check if browser is navigating to another page or closing. My idea is;
- 创建全局变量
var isNavigating = false;
- 绑定单击事件以使将导航到另一个页面的每个锚点的
isNavigating = true
- 检查正文的
unload
事件中isNavigating
是否为真.
- Create global variable
var isNavigating = false;
- Bind click event to make
isNavigating = true
of every anchor which will navigate to another page - Check if
isNavigating
true on body'sunload
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屋!
查看全文