Reputation: 3578
Can anyone come with advice on how to close a tab in javascript that works for all browsers? Certain code snippets work only for certain browsers - anyone have a kind of a universal way that will cover the major browsers?
Upvotes: 2
Views: 12920
Reputation: 114437
In general, only browser windows created using JavaScript can be closed using JavaScript. Otherwise malware would be closing all of our browser windows on us.
Upvotes: 10
Reputation: 19344
As already stated, you can only close windows/tabs that you created... The open in a new tab is a behavior depending on a given browser's settings.
//keep a handle to the window you open.
var newWin = window.open('my window', 'http://.../');
...
//some point later in the code
newWin.close();
Upvotes: 2
Reputation: 146660
If you open a window using JavaScript, you can close it later with window.close(), which has been around since the days of Netscape Navigator. I'm not aware of any other method.
If you didn't open the window in the first place, there's no way for you to close it. It's a security mechanism that (again) has been around since the 1990s. As far as I know, there's no major browser that lacks it.
I can't figure out what kind of snippets you've found out there (maybe some Flash or VBScript?) but there isn't really much more to say about the subject.
Upvotes: 0