wtm wtm - 5 days ago 5
Javascript Question

Javascript close an tab you just opened

I want to open a URL in a new window, and after x seconds or when the page is done loading I want to close it again.

I am now using:

window.open('http://otherurl.com', 'newwindow', 'width=300, height=250'); return false;


The 'otherurl' is not domain, so I cannot control that URL.

How can I do this?

Answer

window.open gives you a reference to that window, so:

var newWindow = window.open('http://otherurl.com', 'newwindow', 'width=300, height=250');

setTimeout(function() { newWindow.close(); }, 5000);
Comments