1

We have two html pages. First Page we are opening the second page using window.open("./pop.html", "",'width=200,height=100');

In the pop.html we are submitting the form automatically and we need to close the HTML after submission. The code is given below.

<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>

But window.close(); not working in page. Is it missing anything?

xlm
  • 6,854
  • 14
  • 53
  • 55
PrabakarK
  • 57
  • 1
  • 3
  • 12

3 Answers3

1

Open and close a window by the window name. Example:

Open window:

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

Close window:

my_window.close();

Or inside the content of my_window:

window.close();
  • Here we have two pages. In the page1 we are opening var my_window = window.open("URL_HERE", "my_window", "height=100,width=100"); and we need to close the second page automatically after login success. – PrabakarK Jul 17 '17 at 10:15
  • Okay so in the popup use window.close() to close the window like I mentoined in my example. –  Jul 17 '17 at 10:54
0

The window close() method should do what you want.

Here's an example of how to use it:

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

<script>
function close_window(){
    close();
}
</script>
Ortund
  • 8,095
  • 18
  • 71
  • 139
Nihal
  • 5,262
  • 7
  • 23
  • 41
0

You can resolve it setting an interval in first window that close second window

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

You have to find exact time for timeout

Sambuccid
  • 166
  • 2
  • 4