<html>
<head>
<script language="javascript">
var thisPage=false;
window.onbeforeunload=function checkLeave(e){
var evt = e ? e : (window.event ? window.event : null); //此方法为了在firefox中的兼容
if(!thisPage)evt.returnValue='离开会使编写的内容丢失。';
}
function sumbit(){
thisPage=true;
document.getElementById('YourFormId').submit();
}
</script>
</head>
<body>
<p><a href='http://www.baidu.com'>baidu.com</a></p>
<form id='YourFormId' action='http://www.baidu.com'>
<input type="button" name="button" id="button" value="提交" onclick="sumbit();"/>
</form>
</body>
</html>
本文介绍了一种使用JavaScript实现的网页离开警告机制及表单提交方法。当用户尝试离开页面时,会弹出提示框提醒用户可能丢失未保存的数据。此外,还提供了一个提交按钮用于触发表单提交。

259

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



