JavaScript Window closed Tutorial

In this section, we will learn what the window closed property is and how to use it in JavaScript.

JavaScript Window closed Property

The `closed` property contains a Boolean value that indicates if the target `window` object is closed or not.

If the returned value of this property is `true` that means the target browser window is closed. But if the value is `false` it means the browser window is still open.

This method is especially is good for situations where we’ve opened a new browser window programmatically (via methods like open()) and want to keep an eye on when that window is closed.

Note: the method is read-only.

Window closed Property Syntax:

window.closed;

Example: using JavaScript Window closed Property

See the Pen using JavaScript Window closed Property by Omid Dehghan (@odchan1) on CodePen.

Here, we used the `closed` property in the `isClose()` function to see if the target browser window is closed or not.

Notes:

  • The `getElementByTagName` method is explained in later section, but in short this is a way of accessing HTML elements in JavaScript. So we used it here to access the `<p>` tag.
  • If you’re not familiar with the `open()` method, please check to the related section.
  • Refer to the event handler section if you’re not familiar with the `onclick` attribute of the `<button>` tags.

Leave a Reply