2010-12-08 162 views
0

喜如何使用javascriptscript打開一個新的窗口,然後關閉當前窗口..打開新窗口的JavaScript

目前

,我只能打開一個新的窗口,但我的當前窗口依然敞開。

代碼:

function newfunction() 
     { 
      window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0"); 
     } 
+0

順便說一句,「windows」標記有不同的含義,我已經從這個問題中刪除了它。 – 2010-12-08 09:08:10

回答

3

打開一個新的窗口和關閉舊聽起來很像改變當前頁面給我。使用location.href = 'newurl'

如果你想改變它的大小等,也可以這樣做。

window.innerWidth = 300; 
window.innerHeight = 300; 
location.href = 'newurl'; 
0

使用像你說的window.open,然後在主窗口window.close()。但是,如果你這樣使用,瀏覽器會問你:「這個網站想關閉這個窗口blabla」。

0

添加window.opener.close();

function newfunction() 
      { 
       window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0"); 
       window.opener.close(); 

      } 

或添加本就index.html文件,只要打開一個新窗口將關閉父窗口

<body onload="window.opener.close()"> 
0

以下是你需要的代碼:

function newfunction() 
{ 
    window.open("index.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 0"); 
    window.close(); 
} 

只有在當前窗口通過sc打開時才能使用ript,否則IE顯示警告對話框和其他瀏覽器可能會簡單地忽略它。