2

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?

oneiros
  • 3,527
  • 12
  • 44
  • 71

3 Answers3

10

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 1
    Yep. There have in the past been a few workarounds for some browsers, like reassigning `opener`, but in general this is considered an exploit and fixed. HTML5 now [mandates](http://dev.w3.org/html5/spec/Overview.html#dom-window-close) this browser behaviour. – bobince Nov 28 '11 at 20:24
2

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();
Tracker1
  • 19,103
  • 12
  • 80
  • 106
0

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360