<script type="text/javascript">
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
或者
<script type="text/javascript">
window.onbeforeunload = function() {return 'Sure to leave?';};
</script>
可参见:http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
同一页面可提示可不提示:<html>
<body>
<a href="javascript:alert('Hello World!');">Warning</a><br/>
<a onclick="NoPrompt();" href="http://www.baidu.com">No Warning</a>
<script>// Allow the user to be warned by default.
var allowPrompt = true;
window.onbeforeunload = WarnUser;
function WarnUser()
{
if(allowPrompt)
{
event.returnValue = "You have made changes. They will be lost if you continue.";
}
else
{
// Reset the flag to its default value.
allowPrompt = true;
}
}function NoPrompt(){
allowPrompt = false;
}
</script>
</body>
</html>
注:jsp中 <a href="http://www.baidu.com"></a> 在链接外部url时,必须加上协议头http://
本文介绍了如何使用JavaScript在用户尝试离开网页时显示确认对话框的方法。通过设置`onbeforeunload`事件,可以自定义警告消息,阻止用户意外地离开页面。此外,还提供了在同一页面中根据不同情况选择性显示警告的示例。
1240

被折叠的 条评论
为什么被折叠?



