简体   繁体   English

与iFrame内容进行通信

[英]Communicating with iFrame contents

There is an iFrame that contains a whole document (<html> to </html>) . 有一个iFrame包含整个文档(<html> to </html>) This document contains some script tags too. 该文档也包含一些script标签。 My question is, can we call the functions of the scripts which are present in the iFrame? 我的问题是,我们可以调用iFrame中存在的脚本的功能吗?

If iframe navigates the same domain then you can use something like this: 如果iframe导航相同的域,则可以使用以下内容:

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

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

您可以通过contentDocument属性访问iframe的内容,但前提是它属于同一域。

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. 如果框架来自不同的域,并且可以控制源代码,则可以使用postMessage在框架/文档之间进行通信。

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 . 对于浏览器的支持, 这里有一些信息,我们确实有一个jQuery插件 ,该插件带有不支持postMessage的浏览器的备用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM