/  Technology   /  How to close the current tab in a browser window using JavaScript
How to close the current tab in a browser window using JavaScript

How to close the current tab in a browser window using JavaScript

   

 Using JavaScript we can open and close current tab in a browser window with JavaScript. Here we are using close() method from JavaScript to close current tab.

 

<!DOCTYPE html>
<html>
<head></head> 
<body>   
  <button onclick="openW()">Click to open website</button>
  <button onclick="closeW()">Click here to close the window</button>
  <script>
    var win;
    function openW() {
     win = window.open("https://www.google.com", "_blank", "width=900, height=786");
    }
    function closeW() {
     win.close();
    }
  </script>
</body>
</html>

 

In The above code there are two buttons “Click to open website” , which calls a function onClick i.e openW(). In function openW() we have passed the website link and the window width and height which is assigned to the variable win. In this function it links the website using the window.open() method in javascript.

 

SImilarly onClicking the “Click here to close the window” button we are calling function which uses the close() method in javascript to close the opened window.

 

How to close the current tab in a browser window using JavaScript

 

How to close the current tab in a browser window using JavaScript

 

Leave a comment