HighDots Forums  

Script in an IFRAME can not call functions defined in the parent document?

Javascript JavaScript language (comp.lang.javascript)


Discuss Script in an IFRAME can not call functions defined in the parent document? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dave Hammond
 
Posts: n/a

Default Script in an IFRAME can not call functions defined in the parent document? - 07-05-2005 , 01:33 PM






In document "A.html" I have defined a function and within the document
body have included an IFRAME element who's source is document "B.html".
In document "B.html" I am trying to call the function defined in
"A.html", but every attempt results in an "is not a function" error. I
have tried to invoke the function using parent.document.funcname(),
top.document.funcname(), and various other identifying methods, but all
result in the above error. Any pointers would be greatly appreciated!

Example code, document "A.html":

<script>
function foo()
{
alert("foo!");
}
</script>
....
<IFRAME src="B.html">No frames?</IFRAME>

Example code, document "B.html":
<script>
parent.document.foo();
// also tried... top.document.foo();
</script>

Thanks in advance!
-Dave H.


Reply With Quote
  #2  
Old   
Razzbar
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-05-2005 , 01:38 PM






Try putting the call to foo in the iframe's body.onload event, and call
it with
top.foo() or parent.foo()

HTH


Reply With Quote
  #3  
Old   
Dave Hammond
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-05-2005 , 01:56 PM



That worked, thanks! Actually I didn't even need to put it in the
onload event; just calling it as top.foo() rather than
top.document.foo() did the trick.

Thanks very much for the quick response!!

-Dave H.


Reply With Quote
  #4  
Old   
Ivo
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-05-2005 , 02:07 PM



"Dave Hammond" wrote
Quote:
parent.document.foo();
// also tried... top.document.foo();
Functions are not properties of the document, but of the window.
Try
parent.foo();
or
top.foo();

You may want to perform some checks first:
if( parent.foo ) { // function exists, so go ahead
parent.foo();
}

Still a nasty error may occur if the parent is not in the same domain as
your iframe'd page, for example if your page is among Google Images results,
it will be loaded in a frame with a different host, and accessing any
property of parent will break the script, unless you wrap the code in a
try/catch block. It is therefore usually safer to put your scripts in the
top-document, and copy the necessary parts to the iframe onload.

hth
ivo
http://4umi.com/web/javascript/








Reply With Quote
  #5  
Old   
Razzbar
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-06-2005 , 02:49 AM



What's especially neat is that you can assign a new function to the top
window from the iframe, then load another document in the iframe, and
the top window's new function is still available.


Reply With Quote
  #6  
Old   
Christopher J. Hahn
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-06-2005 , 03:26 AM



Razzbar wrote:
Quote:
What's especially neat is that you can assign a new function to the top
window from the iframe, then load another document in the iframe, and
the top window's new function is still available.
If I'm right, I believe that's because functions are primitive data
types in JS, like strings and numbers, so they're passed "by value" in
all operations, and not by reference.

So you're really ending up with a copy of the function, the same way
you would end up with a copy of a string or a number.

Granted, functions are *way* more useful to pass around this way than
strings or numbers.



Reply With Quote
  #7  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-06-2005 , 04:40 AM



Christopher J. Hahn wrote:
Quote:
Razzbar wrote:
What's especially neat is that you can assign a new
function to the top window from the iframe, then load
another document in the iframe, and the top window's
new function is still available.

If I'm right, I believe that's because functions are
primitive data types in JS, like strings and numbers,
Functions are object in javascript. And like all other objects they can
have named properties added to them, and assigned values, at any time.

Quote:
so they're passed "by value" in all operations, and
not by reference.
snip

Being objects they are passed by reference only.

Richard.




Reply With Quote
  #8  
Old   
Christopher J. Hahn
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-06-2005 , 05:11 AM



Richard Cornford wrote:
Quote:
Christopher J. Hahn wrote:
Razzbar wrote:
What's especially neat is that you can assign a new
function to the top window from the iframe, then load
another document in the iframe, and the top window's
new function is still available.

If I'm right, I believe that's because functions are
primitive data types in JS, like strings and numbers,

Functions are object in javascript. And like all other objects they can
have named properties added to them, and assigned values, at any time.

so they're passed "by value" in all operations, and
not by reference.
snip

Being objects they are passed by reference only.

Richard.
Not to be argumentative, but if they are passed by reference only then
could you explain why the referant isn't freed, causing an error in the
case described by Razzbar?

I've been too busy to actually test the case, mind you, but I'm fairly
sure he's correct.



Reply With Quote
  #9  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parentdocument? - 07-06-2005 , 07:12 AM





Christopher J. Hahn wrote:


Quote:
Razzbar wrote:

What's especially neat is that you can assign a new
function to the top window from the iframe, then load
another document in the iframe, and the top window's
new function is still available.

Not to be argumentative, but if they are passed by reference only then
could you explain why the referant isn't freed, causing an error in the
case described by Razzbar?
What does pass by reference or value have to do with the case of frames?
If you have a global variable in one frame then it is a property of the
window object of the frame thus if the iframe document does
parent.varName = someExpression
then a global variable in the parent window is set and that variable
does not change if a new document is loaded into the iframe as the
iframe has its own window object with its own variables.
Whether that expression evaluates to a primitive value or a function
object does not matter at all, there is not even a method or function
with arguments involved where the term passing by reference or value
makes sense.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Reply With Quote
  #10  
Old   
Christopher J. Hahn
 
Posts: n/a

Default Re: Script in an IFRAME can not call functions defined in the parent document? - 07-06-2005 , 08:07 AM



Martin Honnen wrote:
Quote:
Christopher J. Hahn wrote:


Razzbar wrote:

What's especially neat is that you can assign a new
function to the top window from the iframe, then load
another document in the iframe, and the top window's
new function is still available.

Not to be argumentative, but if they are passed by reference only then
could you explain why the referant isn't freed, causing an error in the
case described by Razzbar?

What does pass by reference or value have to do with the case of frames?
Assign an object created in a child frame to a property of the parent
window, then navigate away from that document in the child frame and
attempt to manipulate the object in the parent window, and then maybe
you can tell me.
"Can't execute code from a free script."

Quote:
If you have a global variable in one frame then it is a property of the
window object of the frame thus if the iframe document does
parent.varName = someExpression
then a global variable in the parent window is set and that variable
does not change if a new document is loaded into the iframe as the
iframe has its own window object with its own variables.
True, but if the variable is set as a reference (i.e. the object is
passed by reference to the assignment operator) then upon navigation of
the frame the referrant will been freed and the reference broken. In my
experience, further attempts to manipulate that object will result in
the error (in IE at least):
"Can't execute code from a freed script."

Try it out, as above.

This does not appear to be the case with functions or primitive values.

Quote:
Whether that expression evaluates to a primitive value or a function
object does not matter at all, there is not even a method or function
with arguments involved where the term passing by reference or value
makes sense.
It is a function of operators that they receive values of some kind,
whether they be primitive values or references to objects (see also the
statement: ;+; ). I have commonly seen this provision of values to an
operator referred to as "passing". You might call it something else. It
makes no difference.


Quote:
--

Martin Honnen
http://JavaScript.FAQTs.com/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.