我希望在我的应用程序中捕获浏览器关闭事件,并向用户显示一个确认框。 我正在使用JSF 2.0和richfaces 4.0。
我希望在我的应用程序中捕获浏览器关闭事件,并向用户显示一个确认框。 我正在使用JSF 2.0和richfaces 4.0。
window.onbeforeunload = function ()
{
var shallIAlertUser = Do_Whatever(); //get boolen value
if (shallIAlertUser) {
//this will alert user
return 'Are you sure?';
}
else {
//this wont
window.onbeforeunload = undefined;
}
};
beforeunload
事件。window.onbeforeunload = function(event) {
event = event || window.event;
var confirmClose = 'Are you sure?';
// For IE and Firefox prior to version 4
if (event) {
event.returnValue = confirmClose;
}
// For Safari
return confirmClose;
}
请记住,这将会在除了关闭窗口之外的其他事件中触发,例如重新加载和表单提交。
onbeforeunload
< body onbeforeunload="alert('Closing');">
例子:
<html>
<head>
<title>`onbeforeunload` Event Demo</title>
</head>
<body onbeforeunload="return 'Are you sure you want to exit ?';">
</body>
</html>
将事件处理程序绑定到 unload
事件。