23

I want to close current tab using javascript/Jquery. But I did not find any solution. I read that the window.close() only works with window which is open with window.open() method. So is there any way to send command to system that user press ctrl+w which is also close this window. fiddle

function down(){
window.close()
} 
Saksham
  • 9,037
  • 7
  • 45
  • 73
Jitender
  • 7,593
  • 30
  • 104
  • 210
  • read this: http://stackoverflow.com/questions/1419307/close-windows-or-tabs-on-a-button-click – Jean-Paul Sep 17 '13 at 18:37
  • possible duplicate of [Closing the Browser tab using js or jquery](http://stackoverflow.com/questions/8411978/closing-the-browser-tab-using-js-or-jquery) – j08691 Sep 17 '13 at 18:37
  • 4
    I think you can only close a tab that was opened using Javascript. – Daniel Schwarz Sep 17 '13 at 18:37
  • http://stackoverflow.com/a/12896858/2519416 – Martijn Sep 17 '13 at 18:37
  • Long answer, it's not possible. read this [window.close](https://developer.mozilla.org/en-US/docs/Web/API/window.close).`When this method is called, the referenced window is closed.`In you demo, the `window` referenced a iframe, which is embeded in the page.So, the `window.close` is not working. – wener Sep 17 '13 at 18:43
  • 1
    Does this answer your question? [How to close current tab in a browser window?](https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – Heretic Monkey Apr 21 '20 at 16:58

6 Answers6

58

It is possible. I searched the whole internet for this, but once when i took one of Microsoft's survey, I finally got the answer.

try this:

window.top.close();

this will close the current tab for you.

Rohit Arora
  • 2,246
  • 2
  • 24
  • 40
5

This seems to work from a previous thread: https://stackoverflow.com/a/18776480/3642027

However, from my testing, it only works if it's a fresh link opened with target="_blank".

Community
  • 1
  • 1
Kukulza
  • 61
  • 1
  • 4
3

I suggest this little function JS, it works on Chrome 54.0.2840.99

function closeWin() { 
    window.top.close();
}
setTimeout(function(){ 
    closeWin()
}, 1000);
Eckstein
  • 785
  • 9
  • 27
Bernabé Tavarez
  • 197
  • 1
  • 1
  • 6
2

Since the actual answer is obsolete because of security issues the closest working solution i found today is

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

I also recommend to check this thread where i got the code from its many authors: How to close current tab in a browser window?

Mbotet
  • 170
  • 2
  • 17
1

I searched about it, someone wrote you can not close it until unless, you opened by code or pro-grammatically It was convincing but I found later and it worked for me. hope so it help you too.

var win = window.open("about:blank", "_self");

win.close();

sami ullah
  • 925
  • 1
  • 9
  • 15
  • 1
    This answer is fundamentally the same as [this answer from Sept '19](https://stackoverflow.com/a/57970545/215552), and is very similar to the answers on the duplicate 10 year old question, [How to close current tab in a browser window?](https://stackoverflow.com/q/2076299/215552). – Heretic Monkey Apr 21 '20 at 16:59
-1

Here a HTML Code witout javascript:

<a href="about:blank" target="">&#10006;</a>

Here with a nice design:

<a style="color:black; text-decoration:none;" href="about:blank" target="">
  <button style="position: fixed; top:2px; right:2px; border: 1px solid black; background: red; cursor: pointer; z-index: 2;">
    &#10006;
  </button>
</a>