Lan*_*Lan 15 javascript browser jquery events
我想在我的应用程序中捕获浏览器关闭事件并向用户显示确认框.我正在使用JSF 2.0和richfaces 4.0.
Pra*_*sad 17
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;
}
};
Run Code Online (Sandbox Code Playgroud)
使用该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;
}
Run Code Online (Sandbox Code Playgroud)
请记住,除了关闭窗口之外,这将是其他事件的火灾,例如重新加载和表单提交.
onbeforeunload
< body onbeforeunload="alert('Closing');">
Run Code Online (Sandbox Code Playgroud)
示例:
<html>
<head>
<title>`onbeforeunload` Event Demo</title>
</head>
<body onbeforeunload="return 'Are you sure you want to exit ?';">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)