Click to See Complete Forum and Search --> : Calling a Variable - Having some Problems


Tom23
07-25-2003, 10:48 AM
I’m pretty new to JavaScript and thus would appreciate some help on a small problem that’s slowly driving me insane.

I’ve tried a bunch of different ways to call a variable from a parent without any luck. My attempt is below. I need this to call an array from a parent window, but to test this is possible I was trying the simple test below.

Parent Window:

<HTML>

<HEAD>

<SCRIPT LANGUAGE=”JavaScript”>

var tom="hello";

open("tom2.htm","tom2");

</SCRIPT>

</HEAD>

<BODY>
</BODY>

</HTML>

Child Window(tom2.htm):

<HTML>

<HEAD>
</HEAD>

<BODY onLoad="alert(parent.tom)">

</BODY>

</HTML>

All I get on the alert is undefined. Anyone know how to call the variable? Any help would be terribly appreciated.

Khalid Ali
07-25-2003, 12:35 PM
here is a complete example to illustrate communication between a parent and child window.

http://68.145.35.86/skills/javascripts/ParentWinInteractionWithChildWin.html

Tom23
07-31-2003, 08:54 AM
Thanks for the help Khalid, sorry it took so long to respond, I’ve been trying to unscramble your script (as I mentioned, I’m just starting out – very slow going so far!).

Anyway, ended up using the opener object to gain access to the parent array. Hit another snag very quickly.

The script below has all the details. My parent page calls three images t0.gif, t1.gif, & t2.gif. These images are called through my pic array. Each one has an onClick to a new window. This child window calls one of the following images - p0.gif, p1.gif, or p2.gif. What I require is for the pop up window to show p0.gif, when I click on t0.gif and so on. Because of the loop I end up getting p2.gif, no matter what original image I click on. I understand this is due to the loop, finishing on 2, but I’m still getting stuck.

Talk about frustrating. To try and fix this I’ve created a new variable ‘picClicked’, but I’m still having the problem as I can’t call the variable before it updates through the loop.

I understand this is a crap explanation, but I’m hoping once you check the script, you’ll see what I’m trying to do.

Parent Window(tom.htm):

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--

//New Window Function.
function openNew()
{
open("tomQ.htm","")
}

//Array.
pic=new Array();
pic[0]= 0;
pic[1]= 1;
pic[2]= 2;

//Pic's
var picClicked;

for (i=0; i<3; i++)
{
var temp=pic[i]
document.write("<IMG SRC='t"+temp+".gif' onClick='picClicked=temp;openNew();'>");
}
//-->
</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>

Child Window(tomq.htm):

<HTML>
<HEAD>

<SCRIPT LANGUAGE="javascript">
<!--

//Opening Image from Parent Array.

document.write("<IMG SRC='p"+opener.picClicked+".gif' onClick='window.close();'>");

//-->
</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>

In the child window (tomq.htm) the picClicked variable is always the final number from the loop. I need to somehow call the 'temp' variable used in each image.

Once again any help would be really helpful.

Thanks

Tom23