有没有一种方法可以从JavaScript中捕获浏览器关闭事件?我不想在Page Unload事件或任何事情中这样做。必须通过单击浏览器关闭按钮进行处理。在JavaScript中关闭处理浏览器?
可能吗?
有没有一种方法可以从JavaScript中捕获浏览器关闭事件?我不想在Page Unload事件或任何事情中这样做。必须通过单击浏览器关闭按钮进行处理。在JavaScript中关闭处理浏览器?
可能吗?
不可以。您只有onbeforeunload
事件,可用作原始javascript(jquery不包含此事件)。
如果你想尝试一下,试着在这里发布一个答案,并且不用按“发布你的答案”,尝试关闭浏览器窗口。
这是访问“关闭窗口”事件的最近路线。
不是。页面卸载事件是你所有的。即使这并非总是如此。它会提供干扰用户愿望的能力。
以下是有关这个好文章从4guysfromrolla
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return message to display in dialog box;
}
</script>
onbeforeunload事件捕捉每一个卸载事件,但有一些窍门的方式通过点击关闭按钮来处理,如果用户关闭浏览器,这里是一个示例代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
//handle if your mouse is over the document body,
//if not your mouse is outside the document and if you click close button return message or do what you want
var out = false;
$("body").mouseover(function(){
out=false;
}).mouseout(function(){
out=true;
});
$(window).bind('beforeunload', function(e){
if(out)
{return "Do you really want to leave this page now?"}
});
});
</script>
</head>
<body>
<p>
<a href="http://cssbased.com">go outside</a>
<br>
<a href="test.html">reload page</a>
<span> </span>
</p>
</body>
</html>
不是“否”有点误导性答案? – 2015-03-16 17:01:07