从父级 JavaScript 访问跨域 IFrame DOM 属性
已经有一些类似的问题,但它们往往涵盖孩子与父母的关系。
另一方面,我希望能够获取 IFrame 的 DOM 的一些属性。我不想操纵任何东西。我只想能够读取属性或 DOM 节点。诸如属性或内容之类的东西。
AFAIK 这是不可能的,除非我使用类似 window.postMessage
的东西。该解决方案合理,它基于事件侦听器。这需要在 IFrame 内定义一个侦听器,这意味着更改 IFrame 的代码。我想避免这种情况。
如果您想进行跨域 Ajax 调用,您可以(如果您也有访问权限)设置目标服务器的标头以允许这样做:
Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:POST,GET,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin:*
有趣的是,这不适用于与 IFrame 的通信。所以我的问题是:
(如何)我可以访问跨域 IFrame 的 DOM 属性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我什至无法告诉你我遇到过多少次这样的问题。
阅读此社区 wiki,了解如何规避同源策略,以查找适合您的解决方案。它是我在互联网上找到的最好的同源资源之一。
yayQuery 的 Alex Sexton 还整理了一个 针对一些不同方法的截屏
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
我会尝试http://easyxdm.net/wp/
使用过很多次,非常容易使用,也可以在旧浏览器中使用
i would give a shot to http://easyxdm.net/wp/
used it many times, quite easy to use and works in older browsers too