Anu Anu - 2 months ago 8
HTML Question

close a new pop up window in html

I'm using a form with submit button in html which opens a new window onclick of submit. Now i want this new window to be closed without user intervention.

i have tried below:

<script type="text/javascript">
var newWin;
function newWindow()
{
alert('in newWindow');
newWin=window.open('http://someurl','_blank');
setTimeout(function(){newWin.close()}, 10);
alert('closed..');
}
</script>


but this does seem to close the new window. It still stays open. Pls help.

Answer

Your Code is working Fine, only thing is you need to give some time to see that window, alert boxes not required

<script type="text/javascript">
var newWin;
function newWindow()
{
    newWin=window.open('http://www.google.co.in','_blank');
    setTimeout(function(){newWin.close()}, 5000);
}
</script>
<button onclick="newWindow()">open Window</button>