17

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

Oomph Fortuity
  • 5,710
  • 10
  • 44
  • 89

3 Answers3

35

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.

Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
26

Use this to access your frame function:

    document.getElementById('myFrame').contentWindow.abc()
radia
  • 1,456
  • 1
  • 13
  • 18
2

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

T4NK3R
  • 4,245
  • 3
  • 23
  • 25