1 頁 (共 1 頁)

[javascript]如何動態新開網頁訊息並於幾秒後關閉

發表於 : 2008年 11月 27日, 11:04
tim
利用 window.open 方法, 傳回的視窗物件, 動態寫入資訊, 並配合 setTimeout 進行關閉即可. 要注意的是該傳回的視窗物件必需是全域變數哦(若不特別宣告, 亦為全域變數)!

程式如下,
function test()
{
myWindow = window.open("", "tinyWindow", 'width=150, height=110');
myWindow.document.write("this window will close in 5 seconds!");
setTimeout("myWindow.close()", 5000);
}

代碼: 選擇全部

<html>
<body>
<script>
var myWindow;
function test()
{
 myWindow = window.open("", "tinyWindow", 'width=150, height=110');
 myWindow.document.write("this window will close in 5 seconds!");
 setTimeout("myWindow.close()", 5000);
}
</script>
<input type=button value='test' onclick='javascript test();'>
</body>
</html>