Open In App

Javascript Window Open() & 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 that are assigned to the parameter. 

Syntax:



window.open(URL, name, specs, replace)

Parameters: This method accepts four parameters as mentioned above and described below:

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.




<!DOCTYPE html>
<html>
  
<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:

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.


Article Tags :