How to identify if the child window has been closed in javascript?

让人想犯罪 __ 提交于 2019-12-11 18:36:19

问题


i have this page which opens up a popup. i want to refresh the parent after popup has been closed.. i used the function i found in stackoverflow

  var win = window.open("popup.html");

function doStuffOnUnload() {
     alert("Unloaded!");
  }

if (typeof win.attachEvent != "undefined") {
    win.attachEvent("onunload", doStuffOnUnload);
} 
else if (typeof win.addEventListener != "undefined") {
    win.addEventListener("unload", doStuffOnUnload, false);
}

which didn't do anything after i closed the pop up... what can i do achieve this? thanks...


回答1:


You wrote the unload event in the wrong place. You added an unload event to the main page instead of to the popup...

 var win = window.open("popup.html"); // O.K.

All of this should be in the popup.html page...

function doStuffOnUnload() {
     alert("Unloaded!");
  }

 if (typeof win.attachEvent != "undefined") {
   win.attachEvent("onunload", doStuffOnUnload);
 } else if (typeof win.addEventListener != "undefined") {
  win.addEventListener("unload", doStuffOnUnload, false);
  }

You can use the unload jquery function:

$(window).unload(doStuffOnUnload);

unload docs



来源:https://stackoverflow.com/questions/9248371/how-to-identify-if-the-child-window-has-been-closed-in-javascript

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