Why js onunload is invoked when popup page opens?

拈花ヽ惹草 提交于 2019-12-23 20:01:20

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/4719416/why-js-onunload-is-invoked-when-popup-page-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!