Wednesday, October 1, 2014

From a parent window, one may close a child window which has been spawned from the parent with JavaScript.

var child = window.open("http://www.example.com/", "_blank");
setTimeout(function() {
   child.close();
}, 5000);

 
 

Also: Steve Filtcroft tweeted me suggesting it might be good to sanity check if there is really a child window open to be closed like so:

var child = window.open("http://www.example.com/", "_blank");
setTimeout(function() {
   if (child) {
      child.close();
   }
}, 5000);

 
 

A circumstance like this did come at work today. What if the child page has been closed on its own? ...might be the easiest circumstance I could suggest for why the first blob of code might fall down I suppose.

No comments:

Post a Comment