您的位置:首页 > Web前端 > JavaScript

针对onbeforeunload事件浏览器兼容方法,支持所有浏览器

2014-07-23 16:06 309 查看
<!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" />
<title>onbeforeunload</title>

<!--第一种方法(刷新)-->
<script language="javascript">
window.onbeforeunload = function (e) {
e = e || window.event;// For IE and Firefox prior to version 4
if (e) {
e.returnValue = '页面信息是否保存,确定离开当前页面吗??';
}// For Safari
return '页面信息是否保存,确定离开当前页面吗??';
};
</script>

<!--第二种方法-->
<script type="text/javascript">
window.onbeforeunload = function(e){
if(window.event){
window.event.returnValue = "页面信息是否保存,确定离开当前页面吗";
return false;
}
}
</script>
</head>

<body>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息