0

I want to unload dependent pop-ups when the main window closes. Main window:

        var dependentWindows = [];

        function closeDependents(){
            dependentWindows.forEach(function(dependent) {
                dependent.close();
            })
            dependentWindows = [];
        }

        function addDependent(dependent){
            if(dependentWindows.indexOf(dependent) < 0) {
                dependentWindows.push(dependent);}
        }

        function removeDependent(dependent){
            var position = dependentWindows.indexOf(dependent);
            if(position > 0) {
                dependentWindows.splice(position,1);}
        }


        Event.observe(window,"unload",closeDependents);

dependent pop-up window:

        function addDependent(dependent){
            window.opener.addDependent(dependent);
        }
        function removeDependent(dependent){
            window.opener.removeDependent(dependent);
        }
        Event.observe(window, "load", (function() {addDependent(window);});
        Event.observe(window, "unload", (function() {removeDependent(window);});

None of this is firing....?

Ken Otwell
  • 345
  • 3
  • 13
  • Are the child windows in the `dependentWindows` array? Is the `unload` event firing and calling your handler? You need to have done some level of troubleshooting first and tell us more about where the problem is. – jfriend00 Jul 29 '15 at 14:54
  • Can we see some HTML so we know what you are working with? – Dean.DePue Jul 29 '15 at 14:54
  • Argg!!!! I left out the very last ')' in the popup code.... works perfectly now. Sheesh... – Ken Otwell Jul 29 '15 at 16:49

1 Answers1

0

I want to unload dependent pop-ups when the main window closes

A simple and easiest solution could be to include a small script in each of your child windows, which will periodically poll to see if its opener window is still alive.

Something like this:

setInterval(checkIfOrphaned, 2000);
function checkIfOrphaned() {
    if (!window.opener || (window.opener == 'null') || (window.opener.closed)) {
        window.close();
    }
}
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • Thanks - that would work. But i want to keep a list of child windows for another purpose as well. So now my version works - after fixing the syntax. – Ken Otwell Jul 29 '15 at 16:50