跳转新页面后的子窗口关闭时如何刷新原父窗口?
Got it, let's break down why this is happening first: when you click the link in your initial popup window to navigate to second_page.html, you're loading that new page in the same popup window instance—not opening a brand new popup. Even though you copied the unload code over, sometimes direct assignment of window.onunload can be unreliable in this scenario (due to page loading timing or browser event handling quirks).
Here are a few solid solutions to get the parent window refresh working again:
方案1:用addEventListener绑定unload事件(推荐)
Instead of directly assigning to window.onunload, use the more robust event listener approach, and add checks to make sure the parent window still exists before trying to refresh it. Add this to second_page.html:
window.addEventListener('unload', function() { // 确保父窗口存在且未被关闭 if (window.opener && !window.opener.closed) { window.opener.location.reload(); } });
This method avoids overwriting any existing event handlers and adds a safety net to prevent errors if the parent window was closed accidentally.
方案2:点击链接时打开新的弹出窗口
If you don't mind opening a new popup instead of navigating within the existing one, modify your link to use window.open explicitly. This way, each popup page has its own direct reference to the parent window:
<a href="second_page.html" onclick="window.open(this.href, 'popup', 'width=600,height=300'); return false;">click here</a>
The return false prevents the original link from navigating in the current window, so the new popup opens instead. Your existing unload code will work as expected here.
方案3:改用beforeunload事件
Some browsers have stricter handling of the unload event. Switching to beforeunload (which triggers right before the window closes or navigates away) might be more reliable:
window.addEventListener('beforeunload', function() { if (window.opener && !window.opener.closed) { window.opener.location.reload(); } });
Note: This will also trigger a parent refresh if the user refreshes the popup window itself, so only use this if that behavior is acceptable for your use case.
内容的提问来源于stack exchange,提问作者Roy

