Open In App
Related Articles

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

Improve Article
Save Article
Like Article

In this article, we will see how to close the current tab in a browser window using JavaScript. To make this, we will use window.close() method.  This method is used to close the window which is opened by the method



But accounting for a security feature, which was introduced a few years ago, ordinary JavaScript lost its privilege to close the current tab, just by using this syntax. 

Note: A current window/tab will only get closed if and only if it was created & opened by that script. Means the window.close syntax is only allowed for the window/tab that is created & opened using method

Example: This example shows how to open the GeeksforGeeks window and then close it. 


<!DOCTYPE html>
<html lang="en">
        How to close current tab in a
        browser window using JavaScript?
<body style="text-align: center;">
    <h2 style="color:green;">
    <h4 style="color:purple">
        Close Tab/Window using JavaScript
    <button onclick="openWin()">
        Click to open GeeksforGeeks website
    <button onclick="closeWin()">
        Click here to close the window
        let myGeeksforGeeksWindow;
        function openWin() {
            myGeeksforGeeksWindow = window
                "_blank", "width=786, height=786");
        function closeWin() {



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Jun, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials