Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable value as a variable name 2

Status
Not open for further replies.
Mar 3, 2006
25
In other programming languages I've used you can the value of one variable as a variable name.

For example:
I'm tracking child windows and want to be able to send the name of the window I want to open to a function.
----------------------------------------------
function f_open_child(x) {
x=window.open('','', 'width=200,height=200,left=200,top=200');
}

var x = "test123";
f_open_child(x);
x.document.write('This is a test window.');
----------------------------------------------

In this case the name of the window becomes 'x' and I'd like it to be 'test123' so I can have multiple windows and control what is going to each.

Any suggestions?
Tusen Tuk
 
If you mean you want a variable declared with the name you specify, holding a pointer to the window, then do this:

Code:
function f_open_child() {
	return(window.open('', '', 'width=200,height=200,left=200,top=200'));
}

var test123 = f_open_child();

If you want the window name to be the name you specify, then do this:

Code:
function f_open_child(winName) {
	window.open('', winName, 'width=200,height=200,left=200,top=200');
}

f_open_child('test123');

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
So building on Dan's suggestions, here is a minor change to gain some added functionality...
Code:
function f_open_child(winName) {
    return(window.open('', winName ? winName : '', 'width=200,height=200,left=200,top=200'));
}
You have the ability to call the window without passing a parameter - or you can pass a name for the window. This will allow you to open multiple popups (each needs a unique name). You can overwrite a popup (or create it if it doesn't) by using the same window name as another popup uses.
Code:
var x = f_open_child('ThisWindowName123');
x.document.open();
x.document.write('This is a test window.');
x.document.close();

var y = f_open_child('ThisWindowName456');
y.document.open();
y.document.write('This is a test window as well.');
y.document.close();

var z = f_open_child('ThisWindowName123');
z.document.open();
z.document.write('This is should load into the first test window.');
z.document.close();
Have fun!

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Excellent suggestions the both of them and it's getting closer to what I need. What if I want to take f_open_child a step further and add some initial content at the same time I'm opening the window and then be able to send stuff later based on user input.
----------------------------------------------------
function f_open_child(winName) {
return(window.open('', winName ? winName : '', 'width=200,height=200,left=200,top=200'));
winName.document.open();
winName.document.write('Lots of initial content.');
winName.document.close();
}
----------------------------------------------------
and then
----------------------------------------------------
var y = f_open_child('ThisWindowName456');
y.document.getElementById('someInputText').value='whoya';
len=y.document.getElementById('someSelectBox').options.length;
y.document.getElementById('someSelectBox').options[len]=new Option('moreText','moreValue');
y.someArrayName['A']['02']['descr']='JohnBoy';
----------------------------------------------------

Any reason that shouldn't work. Any suggestions as to some good websites for cross window techniques/tutorials etc . .

Rois
 
I had tried the 'new Option' command a couple weeks ago. It would work sporadically and then crash IE. (I had to restart windows when it happened.) I'm sure I had something wrong but finally just gave up and used the write command to build the options. I'm kind of new to using javascript to build and access elements in a child from a parent (or vice versa). Some of it seems to make perfect sense that it would work but doesn't or doesn't completely.

Anyway, I appreciate your willingness to help me.

Rois
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top