How to Check if a Popup Window Is Closed Using JavaScript

Learn how to detect when a popup window is closed in JavaScript using the window.closed property in this simple guide.

387 views

To check if a popup window is closed in JavaScript, use the `closed` property of the window object. For example: `if (myWindow.closed) { console.log('Popup window is closed'); }`. This property returns true if the window has been closed; otherwise, it returns false. Make sure `myWindow` is the reference to the popup window that you initially opened with `window.open()`.

FAQs & Answers

  1. How do I open a popup window in JavaScript? You can open a popup window in JavaScript using the window.open() method, which allows you to specify the URL, window name, and window features.
  2. What is the window.closed property in JavaScript? The window.closed property returns a boolean indicating whether the referenced window has been closed (true) or is still open (false).
  3. Can I detect if a popup window is closed from the main window? Yes, by keeping a reference to the popup window returned by window.open(), you can use the window.closed property to check its status from the main window.