Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 205
Rory Avatar asked Jun 18 '10 11:06

Rory


People also ask

How do you call an iframe function?

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property. document. getElementById("resultFrame"). contentWindow.

How do you call my function in JavaScript?

The JavaScript call() Method It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What are 2 ways to call a JavaScript function?

Calling a Function through call() and apply() While working with JavaScript functions you've to keep in mind that the functions can have their own properties and methods and call() and apply() being the two of such methods.


1 Answers

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.

like image 135
David Hedlund Avatar answered Sep 21 '22 17:09

David Hedlund