Open In App

JavaScript window.close() Method

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 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




<h2>Window.close() object GFG</h2>
<button class="btn open">Open</button>
<button class="btn close">Close</button>
<script>
    var newPage = document.querySelector('.open')
        .addEventListener('click', function () {
     
            newWindow =
                window.open('https://www.geeksforgeeks.org/',
                '_blank',
                'width=400,height=400 top=100,left=800');
    });
     
    document.querySelector('.close')
    .addEventListener('click', function () {
        newWindow.close();
    });
</script>


Output:

 

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.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads