Call Javascript function from another window

  • Rodie
  • Student
  • Student
  • Rodie
  • Posts: 75
  • Loc: California

Post 3+ Months Ago

Hi all :D

I have a webpage that opens a new window and calls a function of this new window:
Code: [ Select ]
new_window = window.open("page2.php", "window2", "height=120");
new_window.test();
  1. new_window = window.open("page2.php", "window2", "height=120");
  2. new_window.test();

Here is the body of page2.php:
Code: [ Select ]
<body>
 <script language="javascript">
  function test() {
   alert("in test...");
  }
 </script>
</body>
  1. <body>
  2.  <script language="javascript">
  3.   function test() {
  4.    alert("in test...");
  5.   }
  6.  </script>
  7. </body>
The new window is correctly opened but the function call does not work !!! It says: Object does not support this property or method.
I know I suck but I really cannot figure out the problem here...

Thanks for your help :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • FullOfQuestions
  • Graduate
  • Graduate
  • FullOfQuestions
  • Posts: 108

Post 3+ Months Ago

Why not just have the function execute in the new window??

Code: [ Select ]
new_window = window.open("page2.php", "window2", "height=120");


Code: [ Select ]
<head>
<script language="javascript">
  function test() {
   alert("in test...");
  }
  </script>
</head>
<body onLoad="test()">
</body>
  1. <head>
  2. <script language="javascript">
  3.   function test() {
  4.    alert("in test...");
  5.   }
  6.   </script>
  7. </head>
  8. <body onLoad="test()">
  9. </body>
  • Rodie
  • Student
  • Student
  • Rodie
  • Posts: 75
  • Loc: California

Post 3+ Months Ago

Hi Mr. PlentyOfAnswers :)

The code I gave is a simplification of my problem.
I basically cannot the function when I load the new page, but periodically and several times.

But don't you know why my very very function call does not work ???
Thanks for your help
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

This is how you would do it. Passing function arguments is tricky when the function isn't on the same page as the calling statement because of the way functions and variables work in relation to the window object, but its not impossible. I show a very simply way of doing this, but you can certinaly expand it.

test.html:
Code: [ Select ]
<html>
<head>
<script LANGUAGE="JavaScript" TYPE="text/javascript">
    function openWindow(){
        WREF = window.open("test2.html","test2",'width=550,height=650');
        if(!WREF.opener){ WREF.opener = this.window; }
    }

    function getValue(val){
        newVal = 5 * val;
        return newVal;
    }

    var globalGetValue = this.getValue;

</script>
</head>

<body>
<a HREF="javascript:void(0);" onClick="openWindow();">Open the window</a>
</body>
</html>
  1. <html>
  2. <head>
  3. <script LANGUAGE="JavaScript" TYPE="text/javascript">
  4.     function openWindow(){
  5.         WREF = window.open("test2.html","test2",'width=550,height=650');
  6.         if(!WREF.opener){ WREF.opener = this.window; }
  7.     }
  8.     function getValue(val){
  9.         newVal = 5 * val;
  10.         return newVal;
  11.     }
  12.     var globalGetValue = this.getValue;
  13. </script>
  14. </head>
  15. <body>
  16. <a HREF="javascript:void(0);" onClick="openWindow();">Open the window</a>
  17. </body>
  18. </html>


test2.html:
Code: [ Select ]
<html>
<head>
</head>
<body>
<script LANGUAGE="JavaScript" TYPE="text/javascript">
document.write("This is 5 x 7: " + opener.globalGetValue(7));
</script>
</body>
</html>
  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <script LANGUAGE="JavaScript" TYPE="text/javascript">
  6. document.write("This is 5 x 7: " + opener.globalGetValue(7));
  7. </script>
  8. </body>
  9. </html>
  • Rodie
  • Student
  • Student
  • Rodie
  • Posts: 75
  • Loc: California

Post 3+ Months Ago

Hi Carnix

Indeed your code works. Thanks.
The thing is that I would like the parent window to call a JS function in the child window (what you did is the contrary :)).
Anyway, your stuff is useful. Thanks a lot.
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Oh :oops:

Sorry, I mis-read the question.

The same sort of logic should apply. You can't simply call functions outside the document without making them into global objects that operate at the window level instead of the document level.

You might give IRT.org a peruse. I've found that site is less and less useful as time goes on since it doesn't get updated like it used to (actual JavaScript programmers seem to be farther and fewer between these days).

Just be careful not to call the function BEFORE the window is fully loaded, perhaps by using a callback logic that forces your parent script to wait until the child window executes and alters a variable.

Code: [ Select ]
var callbackvar = false;
var sleepcount = 0;
while(!callbackvar){
 sleepcount++;
}
  1. var callbackvar = false;
  2. var sleepcount = 0;
  3. while(!callbackvar){
  4.  sleepcount++;
  5. }


then in your child, you'll have, as the last thing that happens when the page loads, something that does opener.callbackvar = true;

Then, the parent can communicate with a fully loaded child window. You'll need to test and tweek this... I'm writing off the cuff without testing anything... This while loop will run away if you're not careful... for testing, put a more defined end-state in like:

Code: [ Select ]
var callbackvar = false;
var sleepcount = 0;
while(!callbackvar || sleepcount > 100000){
 sleepcount++;
}
  1. var callbackvar = false;
  2. var sleepcount = 0;
  3. while(!callbackvar || sleepcount > 100000){
  4.  sleepcount++;
  5. }


.c
  • Rodie
  • Student
  • Student
  • Rodie
  • Posts: 75
  • Loc: California

Post 3+ Months Ago

Hi Carmix

Indeed I also noticed the problem of loading time (lost my hair on it before figuring it out).

The problem I get is that I wrote a simple test with buttons and I call functions by hitting those buttons (so plenty of time to load). From the children to the parent, the function call works (with "opener"...) but from the child to the parent, NO.

That's what I cannot figure out !
Right now there is no function call in my code anymore, I work directly on the html component of the child page from the parent page.

Anyway one day I'm spend time on it...
Thanks a lot lot lot for your help !! :P
Very useful !!
Good luck

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.