The Wayback Machine - https://web.archive.org/web/20221016165329/https://www.geeksforgeeks.org/javascript-window-close-method/
Skip to content
Related Articles

Related Articles

JavaScript window.close() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jun, 2022
View Discussion
Improve Article
Save Article

In this article, we will learn about the JavaScript window.close() method, which is used for closing a certain window or tab of the browser which was previously opened by the window.open() method. The window.close() method does not require any parameter. We will have some examples to completely understand its working process.

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 temporary variable, and use that variable with the window.close() method to close that page.

HTML




<!DOCTYPE html>
 
<head>
    <title>Window open method</title>
    <style>
        body {
            background-color: #272727;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20%;
        }
 
        .main {
            width: 30%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            background-color: rgb(82, 82, 82);
            margin: 10px;
            font-family: 'Roboto', sans-serif;
            color: rgb(214, 213, 213);
            border-radius: 5px;
            padding: 20px;
        }
 
        .btn {
            width: 100%;
            height: 50px;
            background-color: rgb(29, 29, 29);
            color: white;
            font-size: 20px;
            border: 2px solid rgb(82, 82, 82);
            cursor: pointer;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h2>
            Click the open and close button for
            opening and closing the new window.
        </h2>
        <button class="btn open">Open</button>
        <button class="btn close">Close</button>
    </div>
    <script>
        var newPage = document.querySelector('.open')
            .addEventListener('click', function () {
 
                newWindow =
                    window.open('https://www.geeksforgeeks.org/',
                    '_blank',
                    'width=400,height=400 top=200,left=300');
        });
 
        document.querySelector('.close')
        .addEventListener('click', function () {
            newWindow.close();
        });
    </script>
</body>
 
</html>

Output:

Image

 

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above 
  • Opera 3 and above
  • Safari 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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!