How to close current tab in a browser window using JavaScript? (original) (raw)
Last Updated : 02 Aug, 2024
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 `
How to close current tab in a browser window using JavaScript?GeeksforGeeks
<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>
`
**Output:
Output
Similar Reads
- 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 s 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 2 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 before 2 min read
- How to Stop Browser Back Button using JavaScript ? To prevent users from navigating back to the previous page using the browser's back button, there are a few methods that can be implemented. However, it's essential to note that overriding a user's ability to navigate freely can frustrate them, so these techniques should be used cautiously and only 3 min read
- How to clear form after submit in Javascript without using reset? Forms in JavaScript serve to gather specific user information, essential in various contexts like recruitment or inquiry submissions. While offline forms can be freely distributed, online forms must maintain a consistent structure for all users. After filling out a form, submission is crucial, but d 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 () 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 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 wi 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. Synta 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