Search code examples
javascriptfirefoxonunloadpopupwindow

Why js onunload is invoked when popup page opens?


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

Solution

  • 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.