为什么弹出页面打开时会调用js onunload?

发布于 2024-10-12 15:30:00 字数 1469 浏览 6 评论 0原文

根据文档(我只使用Firefox):

https ://developer.mozilla.org/en/DOM/window.onunload

当 文档已卸载。

但是我下面的代码会触发警报,即使子窗口(即变量名称“win”)刚刚打开,而不是关闭。

alert("failed but still reload:" + win.isSuccess);

"failed but still reload: undefined"

我的注意力是在子窗口关闭时调用 onunload 。我在这里做错了什么?

代码

function showInsertPopup() {
    var ddId = document.getElementById("workRegionDropdown");
    var index = ddId.selectedIndex;
    var workRegionCode = ddId.options[index].value;
    if(workRegionCode != "") {
          var win = window.open(machineFormPopup + "?typeFlag=1&workRegionCode=" + workRegionCode, "window1", "menubar=no, width=700, height=550, toolbar=no");
          win.onunload = function() {
                if(win.isSuccess) {
                      alert("success reload!")
                      getRecordByWorkRegion();
                }
                else {
                      //here gets print out somehow
                      alert("failed but still reload:" + win.isSuccess);
                      getRecordByWorkRegion();
                }
          }//end inner function onunload
     }
 }//end showInsertPopup()

子窗口页面只有简单的js:

window.isSuccess = 1;
window.close();

According to the documentation (I'm only using Firefox) :

https://developer.mozilla.org/en/DOM/window.onunload

The unload event is raised when the
document is unloaded.

But my code below would trigger the alert even though the child window (i.e. variable name "win") has just been opened, not closed.

alert("failed but still reload:" + win.isSuccess);

"failed but still reload: undefined"

My attention is to invoke that onunload when the child window is closed. What am I doing wrong here??

Code

function showInsertPopup() {
    var ddId = document.getElementById("workRegionDropdown");
    var index = ddId.selectedIndex;
    var workRegionCode = ddId.options[index].value;
    if(workRegionCode != "") {
          var win = window.open(machineFormPopup + "?typeFlag=1&workRegionCode=" + workRegionCode, "window1", "menubar=no, width=700, height=550, toolbar=no");
          win.onunload = function() {
                if(win.isSuccess) {
                      alert("success reload!")
                      getRecordByWorkRegion();
                }
                else {
                      //here gets print out somehow
                      alert("failed but still reload:" + win.isSuccess);
                      getRecordByWorkRegion();
                }
          }//end inner function onunload
     }
 }//end showInsertPopup()

Child window page has just simple js:

window.isSuccess = 1;
window.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2024-10-19 15:30:00

实际上,您首先会在弹出窗口中看到原始 about:blank 文档的卸载。 (您可以通过查看窗口的位置来验证这一点。)然后,当弹出窗口关闭时,您应该会看到另一个卸载。

You actually see the unload for the original about:blank document in the popup window first. (You can verify this by looking at the window's location.) You should then see another unload when the popup window closes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文