Refactor Code

  • Gravatar
    Closing Popups When the Parent Window is Closed

    by azamsharp on 7/8/2008 6:50:22 PM
  • My friend recently asked me how to close the child windows when the parent is closed. Here is the code that I came up with:
  • // create an array of popup window object. This is a global variable
    var popups = new Array();

    function openPopUp(url,name)
    {
    var popUp = window.open(url,name,"width=100,height=100");
    popups.push(popUp);
    }


    <body onunload="destroyPopUps()">

    function destroyPopUps()
    {
    if(popups.length == 0) return;

    for(i=0; i<popups.length; i++)
    {
    popups[i].close();
    }
    }
  • Refactor it!
Please log in to refactor the code! Login