How to close current tab in a browser window using JavaScript? Last Updated : 02 Aug, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo 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: Output Comment More infoAdvertise with us Next Article How to close current tab in a browser window using JavaScript? S SohomPramanick Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads 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 before 2 min read How To Change The Browser To Fullscreen with JavaScript? In web development, there may be scenarios where you want to display your web application or a specific element in full-screen mode, covering the entire browser window. This can be useful for creating immersive experiences, such as games, presentations, or video players. JavaScript provides a built- 2 min read How to open URL in a new window using JavaScript? In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.The window.open() method is used to open a 3 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 2 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 hi 2 min read How to Create Full-Page Tabs using CSS & JavaScript? Tabs are a common UI pattern used to organize content into multiple sections within a single page. In this article, we'll explore how to create full-page tabs using CSS and JavaScript. Full-page tabs allow users to switch between different sections of content while maintaining a clean and intuitive 3 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 4 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 Yo 3 min read How to Open a Link Without Clicking on it using JavaScript? To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is 1 min read How to Create a Modal Box using HTML CSS and JavaScript? We will learn how to create a modal dialog box using HTML, CSS, and JavaScript. A modal is a pop-up dialog that appears on top of the main content and requires the user to interact with it before returning to the main screen.Approach to Create Modal BoxHTML Structure:Create a button that opens the m 2 min read Like