20

I want to call javascript function (on click()) which is inside Iframe

<a onclick="abc();" href=# >Call Function which is inside Iframe </a>

<iframe id=myFrame>
   <script>
     function abc(){
         alert("I am inside Iframe");
     }
    </script>
</iframe>

It will be better if there is solution in JQuery.

Thank you

2

3 Answers 3

36

You can do that this way:

<a onclick="$('#myFrame')[0].contentWindow.abc();">Call function which is inside of Iframe</a>

Basically, I get a reference to the Iframe. contentWindow is the global (window) object for the iframe, and all global functions in a document are methods of window.

As a note, you can only do this if both pages, the one containing the iframe and the one in the iframe come from the same domain.

Sign up to request clarification or add additional context in comments.

Comments

28

Use this to access your frame function:

    document.getElementById('myFrame').contentWindow.abc()

1 Comment

+1 for not having to mixed jQuery when not needed.
2

You could also give the iframe a name='frameName', and then reference from the parent like this:
onclick="window.frameName.abc()"

1 Comment

doesn't work on chrome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.