Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Basically, I have an iframe embedded in a page and the iframe has some JavaScript routines I need to invoke from the parent page.

Now the opposite is quite simple as you only need to call parent.functionName(), but unfortunately, I need exactly the opposite of that.

Please note that my problem is not changing the source URL of the iframe, but invoking a function defined in the iframe.

1 Answer

0 votes
by (40.7k points)

Suppose your iFrame's id is "targetFrame" and the function that you want to call is targetFunction():

document.getElementById('targetFrame').contentWindow.targetFunction();

You can also access the frame using window.frames instead of document.getElementById like this:

window.frames[0].frameElement.contentWindow.targetFunction(); 

Note: The above option does not work in most of the latest versions of chrome and Firefox.

Browse Categories

...