2

I have two web pages. First.html:

<script> window.onload = function(){ setTimeout(loadAfterTime, 3000) }; function loadAfterTime() { my_window = window.open("Second.html", "","status=1,width=100%,height=100%");}
this.window = previous_window ;
 </script>

Second.html:

<html>
    <head>
        <script>
function closepopup()
   {
      if(false == previous_window.closed)
      {
         previous_window.close ();
      }
      else
      {
         alert('Window already closed!');
      }
   }
</script>
    </head>
    <body>
        <button onclick="closepopup();">
            Close
        </button>
    </body>
</html>

If we load first.html in a browser, after 3 seconds of load, the JavaScript will make a browser window with the URL set into Second.html. I have added a button in the second.html file. If we press that button, I want to delete/remove the previous browser window (first.html). The window.open is working button window.close is not. Please help. Also, the code should perfectly work on mobile devices(Latest Chrome app on Android and IOS). Any help will be appreciated.

  • 1
    Was the first window opened by a script? If not, you won't be able to close it. See [docs](https://developer.mozilla.org/en-US/docs/Web/API/Window/close). However if it was opened by script you could use the standard `window.opener.close()` instead of your own `previous_window` variable. – James Nov 09 '17 at 16:07

1 Answers1

0

First.html

<script> 
window.onload = setTimeout(loadAfterTime, 3000);
function loadAfterTime()
{
    window.open("Second.html","","status=1,width=100%,height=100%");
}
</script>

Second.html

<html>
<head>
<script>
function closepopup()
{
    if(window.opener)
    {
        window.opener.close();
    }
    else
    {
        alert("Window already closed!");
    }
}
</script>
</head>
<body>
<button onclick="closepopup();">Close</button>
</body>
</html>
Phoenix
  • 57
  • 9