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?
Asked
Active
Viewed 307 times
1
-
2and is the iframe source on the same domain? – Joseph May 27 '12 at 12:23
-
3I don't know about you but [how did you miss these from the search?](http://stackoverflow.com/search?q=iframe+communication) – Joseph May 27 '12 at 12:25
3 Answers
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);

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