2017-07-17 112 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); 
} 

你必须找到超时

确切时间第一个窗口的时间间隔