Wednesday, October 1, 2014

Flag a parent page to close a child page in JavaScript.

The JavaScript for my parent page starts out like this:

var childWindow;
var closeChildWindow = function(isToRedirect) {
   childWindow.close();
   if (isToRedirect) {
      window.location = "http://example.com/yin.html";
   }
}

 
 

...and has this somewhere downstream for spawning the child page:

childWindow = window.open("http://example.com/yang.html", "_blank");

 
 

At the child page, we may request that the parent page close the child page like so:

function closePage(isToRedirect) {
   window.opener.closeChildWindow(isToRedirect);
};

 
 

Beyond the simple code here you may want to wrap calls that use the parent or the child in if statements which check to see if the parent or the child is truthy. See this for more.

No comments:

Post a Comment