2017-07-17 54 views
0

我們有兩個html頁面。第一頁我們正在使用window.open("./pop.html", "",'width=200,height=100');如何自動提交表格後關閉Html窗口

pop.html我們正在自動提交表單,我們需要在提交後關閉HTML。代碼如下。

<html> 
<head> 
<script type="text/javascript"> 

function loadForm() { 
    var method = "post"; // Set method to post by default if not specified. 
    var path = "http://localhost:8080"; 
    var form = document.createElement("form"); 
    form.setAttribute("method", method); 
    form.setAttribute("action", path); 

    var hiddenField = document.createElement("input"); 
    hiddenField.setAttribute("type", "hidden"); 
    hiddenField.setAttribute("name", "username"); 
    hiddenField.setAttribute("value", "sai"); 

    var passField = document.createElement("input"); 
    passField.setAttribute("type", "hidden"); 
    passField.setAttribute("name", "password"); 
    passField.setAttribute("value", "sai123"); 

    form.appendChild(hiddenField); 
    form.appendChild(passField); 

    document.body.appendChild(form); 
    form.submit(); 

    window.close(); 
} 

</script> 
</head> 

<body onload="loadForm();"> 
<form> 
</form> 
</body> 
</html> 

但是window.close();不能在頁面中工作。它錯過了什麼嗎?

+0

我認爲由於安全問題,這是不可能的。 – Albzi

回答

0

以窗口名稱打開和關閉窗口。例如:

打開窗口:

var my_window = window.open("URL_HERE", "my_window", "height=100,width=100"); 

關閉窗口:

my_window.close(); 

或者MY-WINDOW14裏面的內容:

window.close(); 
+0

這裏我們有兩頁。在頁面1中,我們打開var my_window = window.open(「URL_HERE」,「my_window」,「height = 100,width = 100」);我們需要在登錄成功後自動關閉第二頁。 – PrabakarK

+0

好吧,在彈出窗口中使用window.close()來關閉窗口,就像我在我的示例中進行人工合成一樣。 – 2017-07-17 10:54:26

0

window close() method應該做你想要什麼。

這裏有一個如何使用它的一個例子:

<a href="#" onclick="close_window();return false;">close</a> 

<script> 
function close_window(){ 
    close(); 
} 
</script> 
0

你可以解決它設置在關閉第二個窗口

var secondWindow; 
function openWindow(){ 
    secondWindow=window.open("./pop.html", "",'width=200,height=100'); 
    setTimeout(function(){ 
     secondWindow.close(); 
    },500); 
} 

你必須找到超時

確切時間第一個窗口的時間間隔