跨域IFrame DOM属性从父JavaScript的访问 [英] Cross-domain IFrame DOM properties access from parent's JavaScript
问题描述
已经有几个类似的问题,但他们往往涉及子女对父母的关系。
另一方面,我想能够获取IFrame的DOM的一些属性。我不想操纵任何东西。我只想要能够读取属性或DOM节点。像属性或内容。
AFAIK这是不可能的,除非我使用 window.postMessage
。这个解决方案基于事件侦听器是合理的。这需要在IFrame中定义一个侦听器,这意味着要更改IFrame的代码。
如果您想要进行跨域Ajax调用,您可以(如果您也有访问权限)设置目标服务器的头文件以允许:
访问控制允许标头:X请求 - 使用
访问控制允许方法:POST ,GET,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin:*
这有趣的是不能用于与IFrame的通信。所以我的问题是:
(如何)可以访问跨域IFrame的DOM属性?
请阅读这篇文章
a href =http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy>社区Wiki 绕过同源政策,找到一个有效的解决方案为你。它是我在互联网上找到的最好的同源资源之一。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.
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.
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:
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:
(How) can I access cross-domain IFrame's DOM properties?
I cant even tell you how many times I've ran into problems like this.
Read this community wiki on 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.
Alex Sexton of yayQuery also put together a screencast on some different methods
这篇关于跨域IFrame DOM属性从父JavaScript的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!