Stupid HTML question.

Hokay, I feel like an idiot.

Window1 calls a jScript function that opens Window2. Window2 has a control that will open Window3.

What I want to do, is whenever a new Window2 is opened is check if Window3 exists and close it.

Window3 is a child of Window2, but I can’t seem to get to it to close it.

I tried putting something like this in the function that opens window2, but no go -

if(!(typeof(window3 == ‘undefined’))){
window3.close();
}
Hope that makes sense.

Thanks

When you open a new Window2, it does not have any children. If any other instances of Window2 spawned children they are not automatically inherited. However, I believe you should be able to maintain all this from Window1. Window1 can call functions in Window2 if it is the one that opened it and they are on the same domain. So the proper way to do this would be for Window1 to query all of the Window2’s it created for their list of children, and then pass all of these children to any newly created Window2’s. It’s been a while since I’ve dealt with this, so calling arbitrary functions in child windows might only be possible from signed scripts, I am not 100% sure.

To add to groman’s answer… window3 is here a variable name, and it won’t automatically get filled in with a reference to the real window3 unless you fill it in so. It’s a variable, furthermore, that is specific to window1 as I’m reading your code.

Now, having window2 pass along a reference to window3 back to window1 seems like a better way to do what you want than groman’s method… something like (after spawning window3 from window2:)

opener.window3 = window3

Thanks both groman and chrisk. I’ll give it another go when I get back to work on Tuesday.

Sorry for the delay in my response. I’m out of town and away from my code. My Wife is doing a triatholon tomorrow and I’m the pit crew.

Thanks again.

Hmmmm. Still not working. Object does not support this method.

I tried this-----

After window3 is created, and and I fire the code in window2 again —

window3 = window2.document.childNodes(0);
window3.close();
I tested for typeof on window3 and it says its an object.

I do not believe that’s what you want to do here.

I don’t have time to write working code right now, but the overall the algorithm should be roughly as follows (fill in the blank and fix errors as necessary):

In Window1:



window2s* = window.open(...);
if(!window2s*.opener){ window2s[i++].opener = this.window; }

function getOtherWindow2Obj(id) {
          return window2s[id];
}
function getOtherWindow2ObjCount() {
          return i; 
}
var globalGetOtherWindow2Obj = this.getOtherWindow2Obj;
var globalGetOtherWindow2ObjCount = this.getOtherWindow2ObjCount;


In Window2:



window3s[j] = window.open(...)
if(!window3s[j].opener){ window3s[j++].opener = this.window; }
function getWindow3Obj(id) {
          return window3s[id];
}

function getWindow3ObjCount() {
          return j;
}

var globalGetWindow3Obj = this.getWindow3Obj;
var globalGetWindow3ObjCount = this.getWindow3ObjCount;

function IterateOverAllWindow3s() {
     for(a = 0; a < this.opener.globalGetOtherWindow2ObjCount(); a++) {
           tempWin2 = this.opener.globalGetOtherWindow2Obj(a);
               if(tempWin2)
               {
                     for(b = 0; b < tempWin2.globalGetOtherWindow3ObjCount(); b++) {
                            tempWin3 = tempWin2.globalGetOtherWindow3Obj(b);
                             // ... Do whatever with it
                     }
                }
     }
}


Now, I haven’t written any serious JavaScript in a really long time so I’m not sure if I even got the syntax right or if this will work, but the main idea is that a function from another window is not callable unless it is stored as a global object and in the window tree there are no way to determine your siblings other than write a function in the parent window that returns them and make it global. This can probably be written much better passing arrays around instead of individual members but that’s a lot less clear and I’m not sure how it works with global objects like that.

Hope this helps…

Didn’t want to leave you folks hanging.

chriskOpener.window3 = window3 doesn’t do it. Another thing I tried is defining a var Window3 when Window1 opens, then set opener.opener.Window3 = Window3 after Window3 is spawned. Then, when Window1 calls for Window2, it passes the Window3 object to the function that opens Window2. I thought it could then check for the existence of Window3. Now worky. Object not defined.

Groman I’m afraid I really don’t see what you are doing there. Seems in your first statements that you are trying to set some sort of loop on an array of objects, but you have no for or while. My windows are not in an array. There will only be 1 window1, 1 window2 and 1 window3 at any time. That’s the whole intent. I also don’t see where you pass the Window3.close() statement.

Let me try to be clearer – And to regress. When the Code in Window1 fires the js function to open Window2, I want it to check for Window3 and close it if it already exists. It does check for an existing Window2 and closes it just fine.

(Code follows, I hope I renamed all the Windows correctly, in RL I am more descriptiive)

Window2 and Window3 are set as global vars in a startup script.

In my main page (Window1) I have this line of code –


       	       <select id="ParcelQueryList" onChange="javascript:QueryTools(ParcelQueryList.value);runat=server>

The QueryTools function is in a javascript file called AXAX.js (maybe the AJAX is screwing me up.) This is the function.


 function QueryTools(QueryType){

if (Window2){
Window3 = Window2.document.childNodes(0);
alert(typeof(Window3)); // alerts an object
if(typeof(Window3) == 'object'){ 
Window3.close(); // fails with a method not found
} 
if (!( Window2.closed)) {
Window2.close();
  }
}	

switch(QueryType) {
	case 'PPI':
	
	Window2 = Window.open("Window2.aspx","Window2");

	break;
//… other cases are in here.
}

Window2.aspx has a control that calls another function in AJAX.js –


function ParcelQuery(input, QueryType){

// I make an AJAX call to the server here  but the var Window3 is set after the call.

var Window3 = window.open("Window3.aspx","Window3");


Seems strange that I can go back to Window1, and close Window2 with no problem. But I can’t find and close Window3 from Window2.

OK, I see that trying to get the childnode of Window2 won’t work because that returns HTML elements and not a window.

If that’s all you want to accomplish then you will just have to save Window3 in a variable somewhere in Window1 before closing Window2 so you know what to close.
Basically along the lines of
In Window1:




if(myWin3)
{
    myWin3.close();
}

Window2 = window.open(...);

....

myWin3 = Window2.getWindow3();
Window2.close();



In Window2:




Window3 = window.open(...);

....

function getW3()
{
     return Window3;
}

var getWindow3 = getW3; 



You might even get away without using a function and just making Window3 global and saving a copy of that in Window1 before closing Window2.

What I wrote before was for a general case of finding siblings in a window tree.

If you’re only maintaining one window each there should never be a reason to ‘find’ the window. You get a reference to it when you create it, and you can hang on to it for as long as your window is open. You can use that reference to close that window at will. If you need it after the parent window is closed you have to copy it to some other window.

In W1:

W2 = window.open(…);

In W2:

W3 = window.open(…);
W3.close() will close W3 but only from W2. If you open another W2 it won’t have any direct access to an already existing W3 (barring a sibling search as in my first posting). If you want to close W2 before closing its W3 you have to pass the reference to W3 to W1, so that W1 can close it.

I think you’re trying to either ‘find’ W3 from some other window. Even if this is somehow possible this is not a good coding practice unless you explicitly save the reference to it for that purpose.

GAAAAAAA.

I know. It should be simple. I just dumbed it down to the point where I put a button on window2 with a simple onClick=Window3.close(); and it doesn’t find the darn thing.

Gonna step back and start from scratch. Something out there is wiping out my Window3 variable/reference I guess.

It doesn’t matter that I create Window3 from a function called in a referenced .js file, does it?

In other words, Window2 calls a function in a file called AJAX.js. That function in turn does the Window3 = window.open(…)

The fact that it’s in a references .js file does not matter, however the fact that it is in a function does! If you simply do



func wOpen()
{
           var window3 = window.open();
}


window3 is not going to be accesible outside that function.



var window3;

func wOpen()
{
           window3 = window.open();
}


solves this problem.

Huh.

Then, as an example, this should work, right? –

I greatly modified what I’m trying to do. This is line for line my current test code.

When func1 fires, it tests for the existence of Window2. It should close it if it exists.

Now Window1 has a button on it that fires func2. I fire func2 and then re-fire func1. As I see it, it should find Window2 and close it.

Doesn’t work. Window2 stays undefined.



var  Window1;
var  Window2;

function func1(){

if (Window2){
 if (!(Window2.closed)) {
 Window2.close()
  }
}

Window1 = window.open("AddressQuery.aspx");
}

function func2(){

Window2 = window.open("RoadList.aspx");

}

Got it.

Thanks everyone.