Javascript Window Open() & Window Close() Method
Last Updated :
11 Jul, 2025
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 used to set the URL of web pages that need to open. If the URL is not set then window.open() method opens a blank window.
- name: It is an optional parameter and is used to set the window name.
- specs: It is an optional parameter used to separate the item using a comma.
- replace: It is an optional parameter and used to specify the URL URL creates a new entry or replaces the current entry in the history list. This parameter returns a boolean value. If this parameter returns true then URL replaces the current document in the history list and if returns false then URL creates a new entry in the history list.
Return Value: This method creates a new window.
Window.close(): This method is used to close the window which is opened by the window.open() method.
Syntax:
window.close()
Parameters: This method does not contain any parameters.
Return Value: This method does not return any value.
Example: The below example illustrates the window.open() and window.close() method in Javascript. If you click on the Open GeeksforGeeks button then the geeksforgeeks.org page opens in a new window and if you click on the close GeeksforGeeks button then the geeksforgeeks.org windows will close.
HTML
<!DOCTYPE html>
<html>
<head>
<title>window open and close method</title>
</head>
<body style="display: flex;
flex-direction: column;
justify-content:center;
align-items:center;">
<div class="container" style="position: relative;
text-align: center;">
<h1 style="color: rgb(18, 154, 18);">
GeeksforGeeks
</h1>
<h3> window open and close method</h3>
<button onclick="windowOpen()">
Open GeeksforGeeks
</button>
<button onclick="windowClose()">
Close GeeksforGeeks
</button>
</div>
<script>
let Window;
// Function that open the new Window
function windowOpen() {
Window = window.open(
"https://www.geeksforgeeks.org/",
"_blank", "width=400, height=300, top=230, left=540");
}
// function that Closes the open Window
function windowClose() {
Window.close();
}
</script>
</body>
</html>
Output:
Supported Browser: The browsers are supported by the window.open() & window.close() method are listed below:
- Chrome version 1 and above
- Edge version 12 and above
- Firefox version 1 and above
- Internet Explorer version 4 and above
- Opera version 3 and above
- Safari version 1 and above
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
Similar Reads
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()
3 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
2 min read
JavaScript window.open() Method The Javascript window.open() method is used to open a new tab or window with the specified URL and name. It supports various parameters that can be used to specify the type and URL location of the window to be opened.Syntax:window.open(url, windowName, windowFeatures)Parameters: It has the following
3 min read
How to close current tab in a browser window using JavaScript? 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 secur
1 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
Window Object in JavaScript In JavaScript, the Window object represents the browser window that contains a DOM document.The Window object offers various properties and methods that enable interaction with the browser environment, including manipulating the document, handling events, managing timers, displaying dialog boxes, an
5 min read