1.用javascript重新定義 window.onbeforeunload() 事件
在javascript裏定義一個函數即可
function window.onbeforeunload() { alert("關閉窗口")}
alert()事件將會在關閉窗口前執行,你也可以用戶決定是否關閉窗口
function window.onbeforeunload() {
if (event.clientX>document.body.clientWidth && event.clientY<0 ||event.altKey)
window.event.returnValue="確定要退出本頁嗎?";
}
2.用onUnload方法
在body 標籤里加入onUnload事件
body onUnload="myClose()"
然後在javascript裏定義myClose()方法
但是onUnload方法是在關閉窗口之後執行,不是在關閉窗口之前執行,如果你想在關閉窗口之前做判斷,請用第一種方法
以上兩個方法要能夠成功關閉窗口,則該窗口必須是獨立的新窗口;如果是基於父窗口,那麼是無法關閉的。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>導出數據</title>
<script type="text/javascript">
if(top.location != self.location )
{
top.location = self.location;
}
var time = 5; //時間,秒
function Redirect() {
//window.location = "http://www.cssue.com/";
window.opener=null;
window.open('','_self');
window.close();
}
var i = 0;
function dis() {
if( i > time )
{
Redirect();
}
document.all.t.innerHTML = "還剩<span style='color:red'>" + (time - i) + "</span>秒,本頁面將自動關閉";
i++;
}
timer = setInterval('dis()', 1000); //顯示時間
timer = setTimeout('Redirect()', time * 1000); //跳轉
</script>
</head>
<body >
<div style="text-align:center;margin-top:5%">
<h1>
<div id="s">Sorry ! 您輸入的信息在服務器中無法找到</div>
<div id="t"></div>
</h1>
</div>
</body>
</html>