35

I have a close link on my web page. I would like to function it to close the current tab when click it. I have written

<a href="#" onclick = "javascript:self.close();">close</a>

The above code seems to be working well in Internet Explorer. But it is not working in Mozilla Firefox and Google Chrome. Please help me to resolve this.

Andreas
  • 21,535
  • 7
  • 47
  • 56
Vinod Kumar
  • 1,479
  • 3
  • 13
  • 23

7 Answers7

47

You can only close windows/tabs that you create yourself. That is, you cannot programmatically close a window/tab that the user creates.

For example, if you create a window with window.open() you can close it with window.close().

Brian Ustas
  • 62,713
  • 3
  • 28
  • 21
  • Thanks for your answer. Probably why it is working in Internet Explorer? – Vinod Kumar Jan 17 '13 at 07:26
  • 3
    Not sure, but IE sometimes has its own ways. – Brian Ustas Jan 17 '13 at 16:07
  • @ustasb How can I do window.open() to have the possiblity to close it later?. I'm really don't understand where should I put window.open, I've put that line of code in my head section inside script tags and it's just open another tab wich makes sense, but I don't want to close the new window open, I want to close the current window. Any advice? – napstercake Aug 08 '15 at 16:45
6

As of Chrome 46, a simple onclick=window.close() does the trick. This only closes the tab, and not the entire browser, if multiple tabs are opened.

Adam M
  • 69
  • 1
  • 2
5

You can use below JavaScript.

window.open('','_self').close();

In a HTML you can use below code

<a href="javascript:close_window();">close</a>

I have tried this in Chrome 61 and IE11 it is working fine. But this is not working with Firefox 57. In Firefox we can only close, windows which opened using below command.

window.open()
Nayana Priyankara
  • 1,392
  • 1
  • 18
  • 26
2

Found a one-liner that works in Chrome 66 from: http://www.yournewdesigner.com/css-experiments/javascript-window-close-firefox.html

TLDR: tricks the browser into believing JavaScirpt opened the current tab/window

window.open('', '_parent', '').close();

So for completeness

<input type="button" name="close" value="close" onclick="window.close();">

Though let it also be noted that readers may want to place this into a function that fingerprints which browsers require such trickery, because Firefox 59 doesn't work with the above.

S0AndS0
  • 860
  • 1
  • 7
  • 20
  • Darn, it seems to operate correctly with Chrome `76` on Linux, well via console tests... @Rubén are ya by chance on a Mac or MS based Operating System? – S0AndS0 Aug 02 '19 at 23:59
  • Maybe I'm doing something wrong (trying to use this on a Google Apps Script web app) – Rubén Aug 03 '19 at 00:32
  • Or may be one cannot depend upon _hacks_ that likely will be patched one day... from what I've read elsewhere JavaScript _shouldn't_ be allowed to close tabs that it's not spawned... though on the solution minded tangent one might be allowed to close any tab from a _plug-in_ scope, however, that would be excessive if only targeting one domain/app as there's many more permissions allowed to plug-ins... I've yet to fully _dive_ into web-apps or browser plug-ins so this is more speculation than experience, well until I do take a _dip_ that is. – S0AndS0 Aug 03 '19 at 05:40
  • @RAS, thanks for reporting with a full version number! Strange that it's not working for `80.0.3987.163` because for `81.0.4044.92` (a higher version) the first JavaScript incantation still works. Are ya by chance trying the HTML variant? – S0AndS0 Apr 09 '20 at 16:10
-1

Try this:

window.open('', '_self').close();
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Anurag Ratna
  • 311
  • 4
  • 11
-2

Use this:

window.open('', '_self');

This only works in chrome; it is a bug. It will be fixed in the future, so use this hacky solution with this in mind.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
-2

Try this:

<script>
    var myWindow = window.open("ANYURL", "MyWindowName", "width=700,height=700");
    this.window.close();
</script>

This worked for me in some cases in Google Chrome 50. It does not seem to work when put inside a javascript function, though.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
IamGuest
  • 1
  • 1
  • 1
  • 1
    That's because `this` refers to `window` in this context, and to the function in the context of a function; `this.window` is just `window`. – Martin Tournoij Sep 03 '16 at 14:04