Close current tab in browser window using JavaScript

Today in this post, we are going to see how to close a current tab in a browser window using JavaScript.

Suppose we have a web page that is opened on a tab of a browser window. Now we want to add a link on that web page so that after clicking the link, the tab that contains the web page will be close. How to do that? Well, here we are going to see that.

There is a JavaScript method available that can close the current window tab. This is javascript close() method that closes the current tab.

Making a simple JavaScript form validator

Disable right click, copy, cut on web page using jQuery

Below is the method that closes the current window tab:

window.close()

Before going to discuss further, I think you should aware of the fact that window.close() method will able to close only the tab that is open by using the window.open() method. So, to close the current tab in javaScript, it must have to be opened by using the window.open() method.

Now suppose you want to create a link that will close the current tab when it will be clicked. To do that lets first create our HTML link:

<a href="#" id="close-window">Close this window tab</a>

The above HTML anchor tag has an id “close-window” that will be used by our JavaScript code which will close the current tab of the browser. The JavaScript code which will do that is given below:

<script type="text/javascript">
  document.getElementById("close-window").addEventListener("click", function(){ 
       window.close();
   });
</script>

Below is the complete example code:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
</head>
<body>

<a href="#" id="close-window">Close this window tab</a>

<script type="text/javascript">
  document.getElementById("close-window").addEventListener("click", function(){ 
       window.close();
   });
</script>
</body>
</html>

Save the above code as an HTML file. After saving it, if you open it on with your browser, you will see a link with the text “Close this window tab“. After clicking this link you will see your browser tab immediately close.

Simple Animation Example Using jQuery

So we have seen how to close the current window tab using JavaScript close() method. The JavaScript close() method is supported by all the major modern browser. Chrome, FireFox, Internet Explorer, Safari and Opera supports this method.

7 responses to “Close current tab in browser window using JavaScript”

  1. Stphen says:

    Doesnt work on latest version of Chrome..
    Stphen

  2. Alibaba says:

    The window.close() only closes the window which is opened by window.open() method.

  3. rjc says:

    Works for me:

    window.onload = function() { window.print(); }
    window.onafterprint = function() { window.close();}

  4. TravelMan says:

    Works nicely! But how can you perform the same from a Button OnClick?

  5. Matthew Wells says:

    I got an error when I submitted this form, I need 40 characters so I can’t just tell you its out dated garbage, instead I have to also tell you its out dated garbage and its out dated garbage. Have a nice day!

Leave a Reply

Your email address will not be published. Required fields are marked *