简体   繁体   English

如何使用Javascript或jQuery从iFrame获取控件

[英]How to get the control from iFrame using Javascript OR jQuery

We are loading the third party iframe in our html page. 我们正在html页面中加载第三方iframe。 We are loading this in one particular div. 我们正在一个特定的div中加载它。 Till this step it is working successfully. 到此步骤为止,它已成功运行。 However, the next thing we want find the controls which are loaded inside the iFrame. 但是,接下来我们要查找在iFrame中加载的控件。 Also, iFrame does not have any id associated to it. 另外,iFrame没有任何关联的ID。

Because of iFrame ID is not present we have tried like below to first get the iframe 由于不存在iFrame ID,因此我们尝试如下所示首先获取iframe

var objiframe = $('#divContainer').find('iframe');

So here we are getting the iFrame object, but we are not able to find any controls which are residing under iFrame. 因此,在这里我们获取了iFrame对象,但找不到位于iFrame下的任何控件。

We have tried few ways like below to find the controls 我们尝试了以下几种方法来找到控件

var iFrameDOM = $(objiframe).contents();
var control = $(iFrameDOM).find("#duedate");

var data = objiframe.contentWindow.document.getElementById('duedate');

But somehow we are not getting the controls or there id. 但是不知何故我们没有得到控件或id。 Most of the times we are getting 'undefined' or 'Cannot read property 'contentWindow' of null' 在大多数情况下,我们会得到“未定义”或“无法读取null的属性'contentWindow'”

Any help on this appreciated 任何对此的帮助表示赞赏

It should not be able to if the pages are from different domains, the browsers security sandbox should prevent this type of access. 如果页面来自不同的域,则应该不能够,浏览器安全沙箱应阻止这种类型的访问。 It might work when the two pages are from different sub domains of the same domain. 当两个页面来自同一域的不同子域时,它可能会起作用。

Accessing the child iframe might work, but the other way around will most certainly not work. 访问子iframe可能会起作用,但是反过来肯定不会起作用。

The easiest way would be through the frames object on window like this: 最简单的方法是通过窗口上的frames对象,如下所示:

window.frames[0].document.getElementById('ElementId').style.backgroundColor="#000";

If the iFrame is on another domain than yours you will be restricted and the only way to communicate with the iframe is to use Web Messaging API . 如果iFrame不在您的其他域上,那么您将受到限制,并且与iframe进行通信的唯一方法是使用Web Messaging API

So you have to check what functions are exposed by the iFrame. 因此,您必须检查iFrame公开了哪些功能。

This Article can help you understand what is possible to do or not with your third-party iFrame. 本文可以帮助您了解第三方iFrame可以做什么或不能做什么。

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

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