Javascript,calling child window function from opener doesn't work
-
Hello! I'm developing a web application that opens a popup using windows.open(..). I need to call a function on the opened window using the handle returned by "window.open", but I'm always getting the error message "addWindow.getMaskElements is not a function", as if it couldn't access the function declared on child window. This is the behavior in both IE and FF. My code looks like this: function AddEmail(target,category) { if(addWindow == null) { currentCategory = category; var left = getDialogPos(400,220)[0]; var top = getDialogPos(400,220)[1]; addWindow = window.open("adicionar_email.htm",null,"height=220px, width=400px, status=no, resizable=no"); addWindow.moveTo(left,top); addWindow.getMaskElements (); } } I've googled and read from different reliable sources and apparently this is supposed to work, however it doesn't. One more thing, the functions in child window are declared in a separate .js file that is included in the adicionar_email.htm file. Does this make a difference? It shouldn't.. So, if anyone has ran into a similar problem, or has any idea of what I'm doing wrong, please, reply to this message. Thanks in advance.
Kenia
-
Hello! I'm developing a web application that opens a popup using windows.open(..). I need to call a function on the opened window using the handle returned by "window.open", but I'm always getting the error message "addWindow.getMaskElements is not a function", as if it couldn't access the function declared on child window. This is the behavior in both IE and FF. My code looks like this: function AddEmail(target,category) { if(addWindow == null) { currentCategory = category; var left = getDialogPos(400,220)[0]; var top = getDialogPos(400,220)[1]; addWindow = window.open("adicionar_email.htm",null,"height=220px, width=400px, status=no, resizable=no"); addWindow.moveTo(left,top); addWindow.getMaskElements (); } } I've googled and read from different reliable sources and apparently this is supposed to work, however it doesn't. One more thing, the functions in child window are declared in a separate .js file that is included in the adicionar_email.htm file. Does this make a difference? It shouldn't.. So, if anyone has ran into a similar problem, or has any idea of what I'm doing wrong, please, reply to this message. Thanks in advance.
Kenia
Here is a good guess. The Function addWindow.getMaskElements (); is being called before it is ready in addWindow. You could try this ... setTimeout("addWindow.getMaskElements()",2000); That will wait two seconds before calling addWindow.getMaskElements().
modified on Friday, January 22, 2010 10:39 AM
-
Here is a good guess. The Function addWindow.getMaskElements (); is being called before it is ready in addWindow. You could try this ... setTimeout("addWindow.getMaskElements()",2000); That will wait two seconds before calling addWindow.getMaskElements().
modified on Friday, January 22, 2010 10:39 AM
-
Thanks! that was exactly the problem. I did what you suggested and it works perfectly now, thanks again
Kenia
Kewl! Uh, there is a better solution. I sugessted the setTimeout just to confirm what the problem was. The problem with the two second delay is you cant be sure it will be ready. You could call addWindow.getMaskElements (); repetively by catching the error and setting time out again. Or you could try this, in the separate .js file that is included in the adicionar_email.htm add this line ... body.onload = "window.opener.iMready()"; in the parent page put this... function iMready(){ addWindow.getMaskElements (); } if there is already an onload function (it might be window.onload body.onload or onload people do it different ways) declared in the .js, you cant have another so add this line to the function onload points to... window.opener.iMready();
-
Kewl! Uh, there is a better solution. I sugessted the setTimeout just to confirm what the problem was. The problem with the two second delay is you cant be sure it will be ready. You could call addWindow.getMaskElements (); repetively by catching the error and setting time out again. Or you could try this, in the separate .js file that is included in the adicionar_email.htm add this line ... body.onload = "window.opener.iMready()"; in the parent page put this... function iMready(){ addWindow.getMaskElements (); } if there is already an onload function (it might be window.onload body.onload or onload people do it different ways) declared in the .js, you cant have another so add this line to the function onload points to... window.opener.iMready();
-
In fact, that about calling repetively the addWindow.getMaskElements(); is what i did, because i didn't want to modify the function already attached to the onload event of the document. Thanks for your time!
Kenia
I'm happy I could help.