Ajay_Sharma
Ajay_Sharma

Reputation: 51

Pass window.open object from 1 browser to another when these 2 are completely independent

I have a requirement where in i open 2 browser windows through 1 window.Let's say A opens B and C. Then in one of the child window (let's say B), i open another window (say D). Now, in this new window (D), i want to access the object of the C, to check whether that window is open or closed.

Can i do that in javascript?

I know that you can pass use window.open and window.opener but that only works in case if browser windows have parent child relation. But in my case, this is not happening.

Any help would be really appreciable.

Upvotes: 1

Views: 2568

Answers (2)

Drew
Drew

Reputation: 4373

Here is a working solution using plain javascript (aka no libraries).

Copy this script into a new blank file and give it a file name of window_manager.js

    var windows = {};
    function openWindow(url, name, features) {
        windows[name] = window.open(url, name, features);
       return windows[name];
    }  

    function closeWindow(name) {
        var window = windows[name];
       if(window) {
         window.close();
        delete windows[name];
       }
    }

function is_window_open(windowName, checkparents){

// check to see if windowName is itself
if(window.name == windowName){
    // it's me!
    return true;
}

if(checkparents == true){

    // check to see if windowName is a direct parent of this window
    if (window.opener != null && !window.opener.closed){
        // there is an open parent window but is it the one we are looking for?

        if(window.opener.name == windowName){
            // it's our parent and it is still open
            return true;
        }
        // it's not but let's ask our parent about the window we are looking for
        if(opener.is_window_open(windowName, true)){
            return true;
        }else{
            return false;
        }
    }
}

// check to see if windowName is a direct child of this window
if (windowName in windows){
    // this window was opened by this browser but is this window still open?
    if(windows[windowName].open && (!windows[windowName].closed)){
        // yes this window is open
        return true;
    }

}

// ask all our children to check their children, etc..
for (var childWindow in windows) {
    if (windows.hasOwnProperty(childWindow)) {
        // when asking children we dont want them asking the parents, that would be an ugly recursive loop
        if(windows[childWindow].is_window_open(windowName, false)){
            return true;
        }
    }
}

// got nobody left to ask
return false;

}

To Use

  1. Include the window_manager.js script in the head section of every page
  2. Give every window a unique name: window.name='someUniqueName';
  3. Use the openWindow('theWindowname', 'someURL', 'windowFeatures') function to open any child windows.
  4. use is_window_open('windowNameToCheckFor', true) function to see if a particular window is open

This function will recursively traverse up the parental chain looking for the existance of the window name specified. At each level (even itself) it will traverse all the children and children's children, etc.. looking for the window name specified. The end result is that you will get a true (if the named window is open) or false (the window is closed or does not exist)

I am not able to copy in the HTML code for the test html files that I created to demo/test with so I put them in the following pastebin: pastebin test file code

Create all 4 html files: a.htm, b.htm, c.htm, and d.htm using the code given in the pastebin. Browse to a.htm which will open windows b & c. window b will open window d. Window d will check for window c and return True. Close window c and refresh window d. It will now return false because c is no longer open

Upvotes: 1

suff trek
suff trek

Reputation: 39777

Yes you can. Let's say your page A opens windows B and C like this:

var b = window.open("childB.html", "_blank");
var c = window.open("childC.html", "_blank");

Then your window b which is page childB.html opens windowd d

var d =  window.open("childD.html", "_blank")

In the javascript code of childD.html you can place code:

var propC = opener.opener.c.propertyOfC;

opener.opener will reference original window A, which can reference .c and all its objects

You will have to add checks for window/property existence (most likely try/catch will be in order)

Upvotes: 4

Related Questions