何时需要使用window.opener.closed属性?opener窗口关闭后window.opener仍可访问的场景是否存在?
window.opener.closed: Use Cases & Scenarios Where It’s Mandatory Hey there, great question about this often-overlooked browser API detail—let’s break it down clearly so you know exactly when and why to use window.opener.closed.
First, let’s address your core confusion: Yes, there are scenarios where the opener window is definitely closed, but window.opener still exists as an accessible object (so you need to check its closed property to confirm the state).
When window.opener Doesn’t Turn Null After Opener Closes
In most modern browsers, closing the opener window will set the child window’s window.opener to null immediately. But there are exceptions where this doesn’t happen:
- Legacy browsers: Older versions of IE (11 and earlier) don’t null out
window.openerwhen the parent closes. Instead, they leave a "dead" window object where all properties exceptclosedthrow errors if you try to access them. - Specific modern browser behaviors: In some cases (like when the opener and child are same-origin, or when the child was opened without
rel="noopener"), browsers might retain a proxy object forwindow.openereven after the parent closes. This object won’t let you interact with the parent’s DOM or run code, but it will let you check theclosedproperty to confirm the parent’s state.
Scenarios Where You Must Use window.opener.closed
There are three key situations where relying solely on checking if window.opener is null isn’t enough—you need to use the closed property:
Prevent errors when interacting with the opener
If you’re building a child window that needs to call methods on the opener (like updating its DOM or sending data), you can’t just checkif (window.opener)—because a dead proxy object might still exist. Instead, use:if (window.opener && !window.opener.closed) { // Safe to interact with the opener here window.opener.updateParentUI(); }This ensures you only act when the opener is actually alive and accessible.
Cross-window communication reliability
When usingpostMessagebetween child and opener, sending a message to a closed opener won’t throw an error, but it also won’t be received. Checkingwindow.opener.closedfirst lets you avoid unnecessary message sends or handle the closed state gracefully (like showing a "Parent window closed" message to the user).Handling user-initiated opener closures
If the user manually closes the opener window (instead of it being closed via code), some browsers might not immediately null outwindow.opener. In this case, the only way to reliably detect the closure is to check theclosedproperty—especially if your child window needs to adjust its behavior (e.g., disable features that depend on the parent).
Quick Recap
- Always check
window.openerexists first (to avoidnullerrors), then verify!window.opener.closedto confirm the opener is alive. - Use
closedwhen you need absolute certainty about the opener’s state, especially in legacy browser environments or when dealing with user-initiated closures.
内容的提问来源于stack exchange,提问作者Anton

