Script to close other tabs or browser

You can only close windows if you have their handle. That means your page needs to have been the one that opened them (or you somehow passed the handle around).

Example:

var winGoogle = window.open('http://google.com', '_blank');
var winBing = window.open('http://bing.com', '_blank');
var winYahoo = window.open('http://yahoo.com', '_blank');

//close the windows
winGoogle.close();
winBing.close();
winYahoo.close();

You could store these new windows in an array, and iterate over the handles when it is time to close them.

var windows = [];
//each time you open a new window, simply add it like this:
windows.push(window.open('http://google.com', '_blank'));

//then you can iterate over them and close them all like this:
for(var i = 0; i < windows.length; i++){
    windows[i].close()
}

Leave a Comment