The Wayback Machine - https://web.archive.org/web/20240919074150/https://www.geeksforgeeks.org/how-to-close-current-tab-in-a-browser-window-using-javascript/
Open In App

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

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will see how to close the current tab in a browser window using JavaScript.

window.close() method

To make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method. 

Syntax:

window.close();

But accounting for a security feature, that 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 window.open method. 

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

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to close current tab in a 
        browser window using JavaScript?
    </title>
</head>

<body style="text-align: center;">
    <h2 style="color:green;">
        GeeksforGeeks
    </h2>
    
    <h4 style="color:purple">
        Close Tab/Window using JavaScript
    </h4>

    <button onclick="openWin()">
        Click to open GeeksforGeeks website
    </button>

    <button onclick="closeWin()">
        Click here to close the window
    </button>

    <script>
        let myGeeksforGeeksWindow;

        function openWin() {
            myGeeksforGeeksWindow = window
                .open("https://www.geeksforgeeks.org", 
                "_blank", "width=786, height=786");
        }

        function closeWin() {
            myGeeksforGeeksWindow.close();
        }
    </script>
</body>

</html>

Output:  

close-window

Output



Similar Reads

Javascript Window Open() &amp; Window Close() Method
The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values assigned to the parameter. Syntax: window.open(URL, name, specs, replace)Parameters: This method accepts four parameters as mentioned above and described below: URL: It is an optional parameter. It is u
3 min read
How to get the size of screen, current web page and browser window using jQuery?
jQuery can be used to get the dimensions of the current page. The dimensions include the screen, viewport and document height, and width. Getting the window dimensions: Window size is the size of the browser window. This is the size that changes when the browser is resized. The windows height and width can be accessed with the height() and width()
2 min read
Difference between window.location.href, window.location.replace and window.location.assign in JavaScript
Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.) All three commands are used to redirect the page t
4 min read
How to remove active nav-tab when click outside of nav-tab in Bootstrap ?
Bootstrap has the class called "collapse navbar-collapse" which collapses the navigation bar when the user changes the screen resolution. The java-script along-with it at the bottom of the code triggers the collapsed menu in such a way that when the user clicks the hamburger icon of the menu and navigates to the required link, it again collapses th
2 min read
How to open URL in same window and same tab using JavaScript ?
Opening a URL in the same window and in the same tab is a common requirement for web developers. This functionality is useful when you want to load a new web page or document into the current browser window and replace the content of the current web page. There are several ways to open URLs in the same window and in the same tab using JavaScript: T
4 min read
How to return true if browser tab page is focused in JavaScript ?
There may be situations when we need to check if a browser tab is focused or not. The reasons for this include: Prevent sending network requests if the page is not being used by the user as this would reduce the amount of load on the server. Also, this would save upon the cost if we are using a paid third-party API.Stop playback of media when the t
4 min read
How to detect browser or tab closing in JavaScript ?
Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The beforeunload event is triggered just before the window i
2 min read
How to close window using JavaScript which is opened by the user with a URL ?
JavaScript does not allow one to close a window opened by the user, using the window.close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script. Syntax:window.close()Approach: The steps below demonstr
2 min read
JavaScript window.close() Method
JavaScript window.close() method, is used for closing a certain window or tab of the browser which was previously opened by the window.open() method. Syntax: window.close();Parameters: This method does not accept any parameters. Example: In this example, at first we will use the window.open() method to open a new window, assign its value to a tempo
2 min read
How to check if a browser tab is currently active or not ?
Sometimes we need to check if the current browser tab is active or not. When you open YouTube and watch a movie for a long in the browser then the auto screen timeout doesn't work but when you open Facebook, after some time the screen turns off due to the screen timeout of the device. So how does YouTube know if a tab is active? In this article, we
3 min read
How to add a browser tab icon (favicon) for a website ?
A browser tab icon, also known as a favicon is a small image that appears before the title of a webpage in the browser tab or bookmark bar to represent a website. It helps in brand recognition, improves user experience, and makes the website stand out among others. Adding a favicon can help users easily identify your website and differentiate it fr
2 min read
How to print the content of current window using JavaScript ?
The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print()Parameters: No parameters are required Returns: This function does not return anything Example: This example rep
2 min read
How to Open a Hyperlink in Another Window or Tab in HTML?
Opening hyperlinks in a new window or tab is a common requirement when designing web pages, as it enhances user experience by allowing users to keep the current page open while accessing additional content. In HTML, this can be easily achieved using the `target` attribute of the anchor (`&lt;a&gt;`) tag. By setting `target="_blank"`, you instruct t
2 min read
Cross-browser window resize event using JavaScript/jQuery
The resize() method is an inbuilt method in jQuery that is used when the browser window changes its size. The resize() method triggers the resize event or attaches a function to run when a resize event occurs. jQuery has a built-in method for window resizing events. Syntax: $(selector).resize(function) This syntax is used for cross-browser resize e
2 min read
How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
An iFrame is a rectangular frame or region in the webpage to load or display another separate webpage or document inside it. So basically, an iFrame is used to display a webpage within a webpage. You can see more about iFrames here : HTML iFrames There may be a variety of reasons for checking whether a webpage is loaded in an iFrame, for example, i
4 min read
How to redirect browser window back using JavaScript ?
Redirecting a browser window back to the previous page using JavaScript is a common task for web developers looking to enhance user navigation. This guide will cover the simple yet important methods available in JavaScript to achieve this functionality, ensuring a smooth user experience. There are several approaches available in JavaScript to redir
2 min read
How to check the current runtime environment is a browser in JavaScript ?
In this article, we will see how to detect the runtime environment in which our JavaScript code is running. Suppose you are building an application in Node.js and need to include that code in a webpage that would be run on a browser. This would cause some functions of a Node application not to work on browsers and vice-versa. Approach: There is no
2 min read
Javascript Window Blur() and Window Focus() Method
JavaScript provides various methods to manipulate browser windows, two of which are window.blur() and window.focus(). These methods are particularly useful when managing the user interface and enhancing the user experience by controlling window focus. Javascript Window Blur()Javascript Window Blur() method is used to remove focus from the current w
3 min read
How to Stretch Elements to Fit the Whole Height of the Browser Window Using CSS?
To stretch elements to fit the whole height of the browser window using CSS, you can use "vh" units, where "1vh" equals 1% of the viewport height, so setting the height to "100vh" spans the entire height. Approach: Using CSS height PropertyIn this approach, we are using the Height property to fit the whole height of the browser. The height does not
1 min read
How to give a div tag 100% height of the browser window using CSS
CSS allows to adjustment of the height of an element using the height property. While there are several units to specify the height of an element. Set the height of a &lt;div&gt; to 100% of its parent container with height: 100%, or use height: 100vh; for a full viewport height, ensuring the &lt;div&gt; spans the entire browser window. Adjust as pe
2 min read
How to make curved active tab in navigation menu using HTML CSS & JavaScript ?
In this article, we will learn about the curved outside in the active tab used in the navigation menu using HTML, CSS &amp; JavaScript. One of the most beautiful and good-looking designs of a navigation menu is the 'Curve outside in Active Tab' design. With the help of the CSS border-radius property, it is very easy to make an inside curve. But whi
6 min read
How to Create Tab Headers using CSS & JavaScript?
Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hiding the others. Prerequisites:HTMLCSSJavaScriptSt
2 min read
How to Open URL in New Tab using JavaScript?
HTML anchor tag is used to open URLs in a new tab using the target="_blank" attribute. In JavaScript, we have an inbuilt method window.open() which is used to open a new browser window or new tab depending on the browser settings. Using window.open() MethodTo open a new tab, we have to use _blank in the second parameter of the window.open() method.
2 min read
Web Window API | Window locationbar property
In Web API Window.locationbar property returns the object of locationbar, for which we can check the visibility. Syntax: objectReference = window.locationbar Example: Check the visibility. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window locationbar property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; }
1 min read
Web Window API | Window scrollbars property
In Web APIWindow.scrollbars property returns the object of scrollbars, for which we can check the visibility. Syntax : objectReference = window.scrollbars Example : Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window scrollbars property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; } &lt;/st
1 min read
Web Window API | DOM Window fullscreen property
The Window interface has a fullscreen property which tells whether the window is displayed in fullscreen mode or not. Syntax: BoolVal = windowReference.fullScreen; Return value: Boolean True: If windows is in fullscreen mode. False: If windows are not in fullscreen mode. Example : &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window
1 min read
Web Window API | Window personalbar property
In Web API Window.personalbar property returns the object of personalbar, for which we can check the visibility. Syntax: objectReference = window.personalbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window personalbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function
1 min read
Web Window API | Window statusbar property
In Web API Window.statusbar property returns the object of statusbar, for which we can check the visibility. Syntax: objectReference = window.statusbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window statusbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function getvisib
1 min read
Web Window API | Window toolbar property
In HTML Window.toolbar property returns the object of toolbar, for which we can check the visibility. Syntax: objectReference = window.toolbar Example: Check the visibility C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; Window toolbar property &amp;lt;/title&amp;gt; &amp;lt;script type=&amp;
1 min read
Web Window API | Window menubar property
In HTML Window.menubar property returns the object of menubar, for which we can check the visibility. Syntax: objectReference = window.menubar Example: This example uses Window.menubar property to check the visibility of menubar object. C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; Window m
1 min read