Method invocation betwee windows.

B

Bowen Simmons

OK. I have a test html page:

"To.html":

<html>
<head>
<title>To</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript">
function Go(aString)
{
alert(aString);
return false;
}
</script>
</head>
<body>
<a href="" id="T1" onclick="return window.Go(id);"> <p>T1
anchor</p></a>
<a href="" id ="T2" onclick="return window.Go(id);"> <p>T2
anchor</p></a>
</body>
</html>
</html>

If I press on either the "T1" or the "T2" link, it brings up an alert
with the text set to either "T1" or "T2". Life is good so far.

Now, I use a second window to open the page:

"From.html"

<html>
<head>
<title>From</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript">
function Go(aString)
{
var vMapWindow = window.open("To.html", "CurrentMap");
vMapWindow.focus();
vMapWindow.Go(aString);
return false;
}
</script>
</head>
<body >
<a href="T1" id="T1" onclick="return window.Go(id)"> <p>T1
anchor</p></a>
<a href="T2" id="T2" onclick="return window.Go(id)"> <p>T2
anchor</p></a>
</body>
</html>

When I press either the "T1" or "T2" links on the "From.html" page,
what is supposed to happen is that the browser is supposed to open a
new window showing "To.html" (which it does), give it the focus (which
it does even without the focus() call, but as I understand it browsers
are not consistent here), and then call the Go() method in the new
window showing "To.html" which should bring up the alert. It doesn't
do this however. In fact, it seems to throw an exception instead,
causing the "From.html" window to attempt to navigate to the
non-existent "T1" or "T2" page.

I am testing this with Safari, but have also tried Firefox and even
old Mac IE 5, and all of them fail the same way so I assume this is
not a browser problem, but a mistake on my part. Anybody know what my
mistake is?
 
L

Lee

Bowen Simmons said:
function Go(aString)
{
var vMapWindow = window.open("To.html", "CurrentMap");
vMapWindow.focus();
vMapWindow.Go(aString);
return false;
}
</script>

The window.open() method returns without waiting for the window to
actually be opened, so at the time that you're trying to invoke
the Go() method in the new window, it doesn't exist yet.

You can confirm this by making vMapWindow a global variable and
adding a third link that invokes vMapWindow.Go('T3'). Push one
of your links to create the window, then wait for the window to
completely open and press the new link. It will work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top