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
.
Try just
parent.myfunction()
Same things but a bit easier way will be How to refresh parent page from page within iframe. Just call the parent page's function to invoke javascript function to reload the page:
Or do this directly from the page in iframe:
Both works.
Some of these answers don't address the CORS issue, or don't make it obvious where you place the code snippets to make the communication possible.
Here is a concrete example. Say I want to click a button on the parent page, and have that do something inside the iframe. Here is how I would do it.
parent_frame.html
iframe_page.html
To go the other direction (click button inside iframe to call method outside iframe) just switch where the code snippet live, and change this:
to this:
Use following to call function of a frame in parent page
Continuing with JoelAnair's answer:
For more robustness, use as follows:
Workd like charm :)