1

There is an iFrame that contains a whole document (<html> to </html>). This document contains some script tags too. My question is, can we call the functions of the scripts which are present in the iFrame?

dda
  • 6,030
  • 2
  • 25
  • 34
Gaurav
  • 8,367
  • 14
  • 55
  • 90

3 Answers3

1

If iframe navigates the same domain then you can use something like this:

var result = document.getElementById("frame").contentWindow.func(args);
console.log(result);

DEMO: http://jsfiddle.net/Mnt3e/

VisioN
  • 143,310
  • 32
  • 282
  • 281
0

You can access the iframe's content via the contentDocument property, but only if it belongs to the same domain.

qwertyboy
  • 1,576
  • 15
  • 25
  • In that case you'll want the `contentWindow` property in order to call the functions of the inner iframe. `theIframeNode.contentWindow.foobar()` should hopefully call the global `foobar` function inside the iframe. – Jonathan Protzenko May 27 '12 at 12:25
0

If the frame is from different domain, and have the control over the source code, you can use postMessage to communicate between the frames / document.

For browser support there is some information here and we do have a jquery plugin with fallbacks for the browsers which do not support postMessage.

Community
  • 1
  • 1