guitarlass
guitarlass

Reputation: 1617

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

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

Upvotes: 0

Views: 4132

Answers (1)

gdoron
gdoron

Reputation: 150253

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

Upvotes: 1

Related Questions