我尝试了许多方法来通过jQuery或JavaScript检测浏览器关闭事件。但是,不幸的是,我无法检测到关闭。该onbeforeunload
和onunload
方法也没有工作。
如何检测的窗口close
,unload
或beforeunload
事件?
我尝试了许多方法来通过jQuery或JavaScript检测浏览器关闭事件。但是,不幸的是,我无法检测到关闭。该onbeforeunload
和onunload
方法也没有工作。
如何检测的窗口close
,unload
或beforeunload
事件?
$(window).unload(function(){var e=confirm("Are you sure you want to exit?");if(e){}})
Answers:
您是否尝试过此代码?
window.onbeforeunload = function (event) {
var message = 'Important: Please click on \'Save\' button to leave this page.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
};
$(function () {
$("a").not('#lnkLogOut').click(function () {
window.onbeforeunload = null;
});
$(".btn").click(function () {
window.onbeforeunload = null;
});
});
第二个功能是可选的,以避免在单击#lnkLogOut
和.btn
元素时提示。
还有一件事,自定义提示在Firefox中将不起作用(即使在最新版本中也是如此)。有关它的更多详细信息,请转到此线程。
通过参考各种文章并进行一些试错测试,最终我提出了这个想法,该想法非常适合我。
这个想法是要检测由关闭浏览器触发的卸载事件。在这种情况下,鼠标将离开窗口,指向关闭按钮('X')。
$(window).on('mouseover', (function () {
window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
return "";
}
var prevKey="";
$(document).keydown(function (e) {
if (e.key=="F5") {
window.onbeforeunload = ConfirmLeave;
}
else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}
else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}
else if (e.key.toUpperCase() == "F4" && (prevKey == "ALT" || prevKey == "CONTROL")) {
window.onbeforeunload = ConfirmLeave;
}
prevKey = e.key.toUpperCase();
});
如果需要自定义消息,那么ConfirmLeave函数将提供弹出的默认消息,然后返回要显示的文本,而不是函数ConfirmLeave()中的空字符串。
在Linux chrome环境下,尝试以下代码对我有用。在运行之前,请确保将jquery附加到文档中。
$(document).ready(function()
{
$(window).bind("beforeunload", function() {
return confirm("Do you really want to close?");
});
});
为简单起见,请按照以下步骤操作:
它应该显示以下图片:
以下脚本将在Chrome和IE上显示消息:
<script>
window.onbeforeunload = function (e) {
// Your logic to prepare for 'Stay on this Page' goes here
return "Please click 'Stay on this Page' and we will give you candy";
};
</script>
铬
IE浏览器
在Firefox上,您会收到通用消息
机制是同步的,因此没有服务器延迟调用将起作用,您仍然可以准备一个模式窗口之类的机制,如果用户决定停留在页面上,则会显示该机制,但是无法阻止他离开。
回应评论中的问题
F5会再次触发事件,Atl+也会触发F4。
如Phoenix所述,请使用jQuery .bind方法,但是为了获得更多浏览器兼容性,您应该返回String,
$(document).ready(function()
{
$(window).bind("beforeunload", function() {
return "Do you really want to close?";
});
});
可以在以下网站找到更多详细信息:developer.mozilla.org
<script type="text/javascript">
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "Are you sure you want to leave this page without placing the order ?";
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
请尝试此代码,这对我来说很好。此自定义消息会进入Chrome浏览器,但在Mozilla中不会显示此消息。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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) {
var ref="load";
$.ajax({
type: 'get',
async: false,
url: 'logout.php',
data:
{
ref:ref
},
success:function(data)
{
console.log(data);
}
});
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>
用于登录的用户关闭浏览器或浏览器选项卡时,它将自动注销用户帐户...
您可以尝试这样的事情。
<html>
<head>
<title>test</title>
<script>
function openChecking(){
// alert("open");
var width = Number(screen.width-(screen.width*0.25));
var height = Number(screen.height-(screen.height*0.25));
var leftscr = Number((screen.width/2)-(width/2)); // center the window
var topscr = Number((screen.height/2)-(height/2));
var url = "";
var title = 'popup';
var properties = 'width='+width+', height='+height+', top='+topscr+', left='+leftscr;
var popup = window.open(url, title, properties);
var crono = window.setInterval(function() {
if (popup.closed !== false) { // !== opera compatibility reasons
window.clearInterval(crono);
checkClosed();
}
}, 250); //we check if the window is closed every 1/4 second
}
function checkClosed(){
alert("closed!!");
// do something
}
</script>
</head>
<body>
<button onclick="openChecking()">Click Me</button>
</body>
</html>
当用户关闭窗口时,将触发回调。