方法:1、使用setTimeout()方法關閉,語法「setTimeout("clock();",1000);」;2、視窗沒有提示自動關閉,語法「this.window.opener=null ; window.close();」。

本教學操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。
第一種:JS定時自動關閉視窗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <script language= "javascript" >
<!--
function closewin()
{
self.opener=null;
self.close();
}
function clock()
{
i=i-1
document.title= "本窗口将在" + i + "秒后自动关闭!" ;
if (i>0)setTimeout( "clock();" ,1000);
else closewin();
}
var i=10
clock();
</script>
|
登入後複製
#第二種:視窗沒有提示自動關閉的js程式碼
1 2 3 4 5 6 | <script language=javascript>
<!--
this.window.opener = null;
window.close();
</script>
|
登入後複製
擴充資料:
IE6-7 JS關閉視窗不提示的方法
方法一:
js 程式碼
1 2 3 4 5 6 7 | function CloseWin()
{
window.opener=null;
window.open( "" , "_self" );
window.close();
}
|
登入後複製
方法二:
open.html
js 程式碼
1 2 3 4 5 | function open_complex_self() {
var obj_window = window.open('close.html', '_self');
obj_window.opener = window;
obj_window.focus();
}
|
登入後複製
close.html
# js 程式碼
另附:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function closeie(){
window.close();
}
function closeie6(){
window.opener=null;
window.close();
}
function closeie7(){
window.open('','_top');
window.top.close();
}
|
登入後複製
【推薦學習:javascript高階教學】
以上是javascript如何自動關閉視窗的詳細內容。更多資訊請關注PHP中文網其他相關文章!