Friday, August 26, 2011

Browser Based Windows Manager

Managing Parent-Child relationships among Browser Windows


The following code demonstrates how to manage the parent-child relationship among browser windows in a way that does not result in accessing properties on closed windows by managing a global list of windows. Features include:

  • Close child windows
  • Alerts in child, parent, root windows
  • Close all windows




window.privateLab = {
    alert: function (msg) {
        window.alert(msg);
    },
    alertParent: function (msg) {
        if (window.opener) window.opener.alert(msg);
    },
    alertRoot: function (msg) {
        globalLab.alert(msg);
    },
    injectText: function (container, text) {
        window.globalLab.injectText(window.document, container, text);
    },
    closeAll: function () {
        window.globalLab.closeAll(null);
    },
    closeChildren: function () {
        window.globalLab.closeAll(window);
    },
    createTopWindow: function (url) {
        return window.globalLab.createWindow(null, url);
    },
    createChildWindow: function (url) {
        return window.globalLab.createWindow(window, url);
    }
};

window.onunload = function () {
    window.globalLab.removeWindow(window);
    window.globalLab.closeAll(window);
};

if (window.opener) {
    window.globalLab = window.opener.globalLab;
} else {
    window.onbeforeunload = function () {
        var children = window.globalLab.getWindows();
        if (children.length) {
            window.globalLab.closeAll(window);
            return "You will lose globalLab!";
        }
    };
    window.globalLab = {
        count: 0,
        windows: {},
        createWindow: function (parent, url) {
            var count, w, windows;
            parent = parent || window;
            url = url || "WindowLab.htm";
            windows = this.windows;
            var id = "WindowLabChildWindow" + ++this.count;
            w = parent.open(url, id, true);
            windows[id] = w;
            return w;
        },
        removeWindow: function (child) {
            if (!child) return;
            delete this.windows[child.name];
        },
        alert: function (msg) {
            window.alert(msg);
        },
        getWindows: function (parent) {
            var result, windows;
            parent = parent || window;
            windows = this.windows;
            result = [];
            for (var name in windows) {
                if (windows.hasOwnProperty(name)) {
                    w = windows[name];
                    if (!w.closed && w.opener == parent) {
                        result.push(w);
                    }
                }
            }
            return result;
        },
        closeAll: function (parent) {
            var w, i, windows;
            parent = parent || window;
            w = this.getWindows(parent);
            for (i = 0; i < w.length; i++) {
                w[i].close();
            }
        }
    };
}

No comments:

Post a Comment