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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing function with window.opener

Status
Not open for further replies.

michelledebeer

Programmer
Oct 26, 2001
22
SE
In my first window I have a function called test().
I open a new window with window.open() and names the first window "main".
I want to access the test()-function in the main-window from my new window, but I can't seem to get it right.
I tried playing around with window.opener.something, but no luck.

Any thoughts?
// Michelle
 
Have you tried.

self.opener.test()


Sometimes people will think that the references are incorrect when in fact something might be wrong with the function itself. To test it, try something like this.

Create a simple test between child and parent before attempting to run a complex function.

ToddWW :)
 
Thanks a million!
A followup question:

If I want the test()-function to print the result in the second page instead of main, how do I do that?

main.html
<script language=&quot;javascript&quot;>
window.name = &quot;main&quot;;
window.open(&quot;second.html&quot;,&quot;secondary&quot;);

function test(){
document.write(&quot;hello&quot;);
}
</script>

second.html
<script language=&quot;javascript&quot;>
self.opener.test(opener.name);
</script>

Regards,
// Michelle

 
Sure, just create the window a little differently. Like this, then reference that name in your functions. The window reference must always be var'd globally so it's available to other functions.

Code:
<script language=&quot;javascript&quot;>
var myWin
myWin = window.open(&quot;second.html&quot;);

function test(){
    document.write(&quot;hello&quot;);
    main.document.forms[0].textfield.value = &quot;Test Value&quot;
}
</script>

second.html
<script language=&quot;javascript&quot;>
self.opener.test(opener.name);
</script>

Hope this helps.. :)

ToddWW
 
Well, it does half the job...
The result of test() ends up in the first window, but I would like it to end up in the second one.
I tried naming the new window &quot;second&quot; and added it to the test()-function:
second.document.write(&quot;hello&quot;);
I just got a &quot;second is undefined&quot;-error...

Any thoughts?
// Michelle
 
Michelle,

I'm sorry. I've read and read over my replies and I don't know what planet I was on. It seems like you're getting close so I just wanted to show you how you can reference that 2nd window and do things like write content, change element values, close the window, etc.. all from the main window script.

As I mentioned before, if you want to access the 2nd window with script from the main window, you need to initialize the variable glogally, outside of a function like this.

<script Language=&quot;JavaScript&quot;>
<!--
var myWin

function blah() {
blah.. blah..
}
//-->
</script>

Then you can open the 2nd window inside of a function, but since you initialized the variable globally you can have access to it elsewhere. Like this..

<script Language=&quot;JavaScript&quot;>
<!--
var myWin

function blah() {
myWin = window.open(&quot;somepage.html&quot;);
}
//-->
</script>

Then, elsewhere in other functions on the main page, you can do things to the 2nd window like this.

<script Language=&quot;JavaScript&quot;>
<!--
var myWin

function blah() {
myWin = window.open(&quot;somepage.html&quot;);
}

function changeVal() {
myWin.document.forms[0].somefield.value = &quot;Hello&quot;
}
//-->
</script>


That's it in a nutshell. Now if you're trying to write content to the 2nd window or call a function from 2nd window to the first window and have the 1st window return a value back to the 2nd window, it's a little trickier.

Give me some details on exactly what you're trying to do and I can help with that.

ToddWW :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top