JavaScript Window close() method

The close() method in JavaScript is used to close a window that was previously opened using the window.open() method. This method does not take any arguments, and simply closes the window on which the close() method is called.

Here is an example of how the close() method works:

const myWindow = window.open('https://google.com', 'my-window', 'width=800, height=600');

myWindow.close();

// this will close the window that was opened with the 'window.open()' method

In the code example above, we first use the window.open() method to open a new window and navigate to the Google homepage. We store the new window in the myWindow variable.

We then use the close() method to close the myWindow window. The close() method does not take any arguments, so we simply call the method on the myWindow window without passing any arguments.

As a result of calling the close() method, the myWindow window will be closed. If the myWindow window was already closed, calling the close() method will have no effect.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: