0

I've tried window.close(); to close the currently opened tab but it does not seem to work. What am I doing wrong? My scope is to run a JavaScript script that automatically closes a manually opened tab.

Thanks.

  • 1
    See [How to close current tab in a browser window?](http://stackoverflow.com/questions/2076299). – f_puras Sep 07 '12 at 12:26
  • 1
    possible duplicate of [How to close current tab in a browser window?](http://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – Paul Turner Sep 07 '12 at 12:29

2 Answers2

0

Typically in javascript (or other flavors of javascript), this is done with...

<a href="#" class="close">Close Window</a>

$(".close").click(function {
    window.close();
});

Note that this will only work on the tab that contains the link that was clicked. It will not close other tabs open in your browser.

I'm giving a +1 for each comment on the question, too.

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Ok, I forgot to mention that I want the tab to close automatically after the script does its job. –  Sep 07 '12 at 14:16
  • That's fine, add the `window.close();` line to the end of the script – Ortund Sep 10 '12 at 07:04
0

You can have a look at this answer how-to-close-current-tab-in-a-browser-window

Have a look also at the comments of this answer, where Ryan Joy says:

"This method is only allowed to be called for windows that were opened by a script using the window.open method." In other words, you can only use JavaScript to close a window/tab that was spawned via JavaScript"

Also notice that:

window.close();

works only for Internet Explorer where a popup message appears asking if user agrees for current tab/window to be closed.

In Firefox, Chrome only works for empty tabs and tabs/windows opened via "window.open()" method as mentioned above.

Community
  • 1
  • 1
ppolyzos
  • 6,791
  • 6
  • 31
  • 40
  • So, for tabs opened manually there is now way to close them via JavaScript on Firefox? –  Sep 07 '12 at 15:43