Rory
Rory

Reputation: 41927

how to call a javascript function in the top frame?

This seems really simple, but how do I call a javascript function that's defined in the top-level html, from a child frame?

top html doc
 - 1st level frame
    - 2nd level frame

my top html doc has a function called testFn(). In the 2nd level frame I have a button with onclick="top.testFn();" but this doesn't call the testFn(). In Firebug if I use a watch to execute top.testFn(); it says TypeError: testFn() is not a function.

Should this all just work - in which case it's a problem with my documents, or is there some other way to call functions in the top-level window?

Upvotes: 6

Views: 12081

Answers (3)

David Hedlund
David Hedlund

Reputation: 129832

Your code looks right. If it says testFn() is not a function then perhaps it isn't. Have you tried setting onload="testFn();" in the <BODY> of your top frame to make sure that works?

There could be some javascript error which leads to the code creating your testFn function not being successfully completed.

Upvotes: 4

Rory
Rory

Reputation: 41927

Actually, that is exactly how you do it.

The trick is not having a syntax error elsewhere in the javascript on that top-level html page :-)

Upvotes: 0

Dez
Dez

Reputation: 5838

Have you tried calling it like this:

onclick="parent.parent.testFn();"

I am assuming you have the 2nd frame inside the 1st frame.

Upvotes: 2

Related Questions