从父级的 JavaScript 访问跨域 IFrame DOM 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6666078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:27:59  来源:igfitidea点击:

Cross-domain IFrame DOM properties access from parent's JavaScript

javascriptiframecross-domain

提问by Michal M

There are already a few similar questions but they tend to cover child-to-parent relationship.

已经有一些类似的问题,但它们往往涵盖孩子与父母的关系。

I, on the other hand, would like to be able to get some properties of the IFrame's DOM. I don't want to manipulate anything. I only want to be able to read properties or DOM nodes. Things like attributes or contents.

另一方面,我希望能够获得 IFrame DOM 的一些属性。我不想操纵任何东西。我只想能够读取属性或 DOM 节点。诸如属性或内容之类的东西。

AFAIK this is not possible unless I use something like window.postMessage. As reasonable as this solution it's based on event listeners. This requires a listener to be defined inside the IFrame and this means changing the IFrame's code. I want to avoid that.

AFAIK 这是不可能的,除非我使用类似window.postMessage. 与此解决方案一样合理,它基于事件侦听器。这需要在 IFrame 内定义一个侦听器,这意味着更改 IFrame 的代码。我想避免这种情况。

If you want to make a cross-domain Ajax call, you can (if you have access too) set target server's headers to allow that:

如果您想进行跨域 Ajax 调用,您可以(如果您也有访问权限)设置目标服务器的标头以允许:

Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:POST,GET,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin:*

This interestingly enough doesn't work for communication with an IFrame. So my question stands:

有趣的是,这不适用于与 IFrame 的通信。所以我的问题是:

(How) can I access cross-domain IFrame's DOM properties?

(如何)我可以访问跨域 IFrame 的 DOM 属性吗?

采纳答案by Greg Guida

I cant even tell you how many times I've ran into problems like this.

我什至无法告诉你我遇到过多少次这样的问题。

Read this community wikion circumventing the same-origin policy to find a solution that works for you. Its one of the best same-origin resources I've found on the internet.

阅读有关规避同源策略的社区 wiki以找到适合您的解决方案。它是我在互联网上找到的最好的同源资源之一。

Alex Sexton of yayQueryalso put together a screencaston some different methods

yayQuery 的Alex Sexton还针对一些不同的方法制作了一个截屏视频

回答by user1555320

i would give a shot to http://easyxdm.net/wp/used it many times, quite easy to use and works in older browsers too

我会尝试http://easyxdm.net/wp/多次使用它,非常易于使用并且也可以在旧浏览器中使用