The closed
property in JavaScript is a read-only property that indicates whether a window is closed or open. This property is a boolean value, with true
indicating that the window is closed and false
indicating that the window is open.
Here is an example of how the closed
property works:
const myWindow = window.open('https://google.com', 'my-window', 'width=800, height=600');
console.log(myWindow.closed); // logs 'false'
myWindow.close();
console.log(myWindow.closed); // logs 'true'
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 console.log()
method to log the value of the myWindow.closed
property to the console. Since the myWindow
window is open, the value of the closed
property is false
.
We then use the close()
method to close the myWindow
window. After closing the window, we log the value of the myWindow.closed
property to the console again. Since the myWindow
window is now closed, the value of the closed
property is true
.
As a result of using the closed
property, we can determine whether a window is open or closed. This can be useful in cases where we want to know whether a window is open or closed, and take appropriate action based on the value of the closed
property.
Leave a Reply