Relative call of parent function

M

moweis

From within a frame, I know you can call a javascript function in the
parent using: parent.parent.functionCall()

Is there a way to this in a relative way (i.e. not absolute)?
For example, suppose a page (the parent) opens an iframe and passes it
a handle to function name of the parent.
Is there a way for the iframe to call that function?

Or to say it another way, is there a way for child page to call a
function on the parent page, where the function name is contained in a
variable?

Thanks
 
H

Henry

From within a frame, I know you can call a javascript function
in the parent using: parent.parent.functionCall()

Is there a way to this in a relative way (i.e. not absolute)?
For example, suppose a page (the parent) opens an iframe and
passes it a handle to function name of the parent.
Is there a way for the iframe to call that function?

Or to say it another way, is there a way for child page to
call a function on the parent page, where the function name
is contained in a variable?

Yes there is a way; chain up the hierarchy of parents testing to see
if one of the window/global objects has a property of function type
with the offending name.

But you mostly should not need to do that as you can pass references
to function objects about as easily as you can pass the string names
of function objects.
 
M

moweis

Yes there is a way; chain up the hierarchy of parents testing to see
if one of the window/global objects has a property of function type
with the offending name.

But you mostly should not need to do that as you can pass references
to function objects about as easily as you can pass the string names
of function objects.


Henry, thanks for the response.

I'm really not clear on the syntax.
For example,
PARENT PAGE has function: parentFunction
an IFRAME gets passed the name of this function into variable:
function_to_call

How does the iframe call the function?
How can I use the variable: function_to_call?
parent.parent.<?>

I guess I'm looking for something like a hash lookup:
parent.parent.getFunctionByName(function_to_call)
 
T

Todd Wade

Thanks Henry! That was a great help.

This led me to a solution that worked well for my situation: using
eval.

in the iframe:
eval('parent.parent.'+function_to_call+'()');

you could use parent.parent[function_to_call]() instead. A little
cleaner.

Todd W.
 
T

Thomas 'PointedEars' Lahn

[...]
How can I use the variable: function_to_call?
parent.parent.<?>
<snip>

Bracket notation. See:-

<URL:http://jibbering.com/faq/faq_notes/square_brackets.html>

Thanks Henry! That was a great help.

This led me to a solution that worked well for my situation: using
eval.

in the iframe:
eval('parent.parent.'+function_to_call+'()');

Don't you think Richard would have suggested eval() if it was better than
bracket notation here?


PointedEars
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top