Javascript 如何使用javascript检测浏览器选项卡刷新或关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14767136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I detect browser tab refresh or close using javascript
提问by Rohitashv Singhal
I have one problem, I have a javascriptfunction which I want to use when the browser is closed, How can I detect that a browser is being closed?
我有一个问题,我javascript想在浏览器关闭时使用一个功能,我该如何检测a browser is being closed?
I have made some research on that I got solution like onunloador beforeunloadbut both the functions are being executed when I am reloading the page, Is there any solution that I can differentiate reload and browser/tab close.
我已经做了一些研究,我得到了类似的解决方案onunload,beforeunload但是当我重新加载页面时,这两个功能都在执行,是否有任何解决方案可以区分重新加载和浏览器/选项卡关闭。
采纳答案by Yatin Trivedi
answer is,
答案是,
</head>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
alert("bye");
}
function wireUpEvents() {
/*
* For a list of events that triggers onbeforeunload on IE
* check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
*/
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
</script>
</head>
<body>
<h1>Eureka!</h1>
<a href="http://www.google.com">Google</a>
<a href="http://www.yahoo.com">Yahoo</a>
</body>
</html>
回答by gdoron is supporting Monica
No, you can't know that, the HTML page isn't the "owner" of the browser, you have only limited access to information, and this info isn't inside it.
不,您无法知道,HTML 页面不是浏览器的“所有者”,您对信息的访问权限有限,而且这些信息不在其中。
You can know when the user leaves your page, but you can't know why, as it's none of your business...
你可以知道用户什么时候离开你的页面,但你不知道为什么,因为这不关你的事......
回答by David 'the bald ginger'
gdoron is correct in that you cannot determine why/how the user is 'leaving the page'. On the extremes you can perhaps determine on mousedown events if the browser's CLOSE button was clicked and let that fire of an alert. But this would probably require tracking the X and Y of the mousedown event and that isn't a very nice way of doing things. And i do not think you would be able to accurately determine if a tab is closed.
gdoron 是正确的,因为您无法确定用户“离开页面”的原因/方式。在极端情况下,如果单击浏览器的 CLOSE 按钮并触发警报,您也许可以确定 mousedown 事件。但这可能需要跟踪 mousedown 事件的 X 和 Y,这不是一种很好的做事方式。而且我认为您无法准确确定选项卡是否已关闭。

