16

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,370
  • 10
  • 37
  • 86

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
  • 17,609
  • 3
  • 24
  • 40
25

Use this to access your frame function:

    document.getElementById('myFrame').contentWindow.abc()
radia
  • 1,406
  • 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,125
  • 2
  • 20
  • 24