从 iframe 传回信息?
现在我正在构建一个 Firefox 插件,它可以复制我网站上的一些功能。它接收电子邮件地址,然后将信息返回给用户。在插件中执行此操作的最简单方法是使用 Iframe 并在我的网站上呈现超级简单的表单。所有这些都很好用,但为了使插件真正有用,我希望插件能够访问 iframe 渲染的信息,这样它就可以在用户所在的当前窗口中使用它。
是否可以通过以这种方式通过 Iframe 返回信息?我知道 Iframe 有相当多的域访问限制,因此我们将不胜感激!
Right now i'm building a firefox plugin that duplicates some functionality on my website. It takes in an email address and then returns information to the user. The easiest way to do this in the plugin is to use an Iframe and render that super simple form on my website. All of this works great, but to make the plugin really useful, i would like the plugin to have access to the information that the iframe renders, so it can use it in the current window that the user is in.
Is it possible to pass information back through an Iframe in this manner? I know there are quite a few domain access restrictions with Iframes, so any help or insight is appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我用两种方法做到了这一点。
如果 iframe 与父网站位于同一域中,您只需在 javascript 中访问 window.parent。
然而,如果不是的话……我就做了一个肮脏的把戏。不过,我会在这里分享它,因为它可能会有所帮助。
我们在另一个域上创建了一个页面,它将调用 window.parent.parent。我们将其放入 iframe 页面内的隐藏 iframe 中,并向其发送一个或两个查询字符串参数。它并不漂亮,但它解决了跨域脚本问题。
这基本上意味着你有这样的事情:
admin.example.com
content.example.com - iframe
admin.example.com?contentid=350 - 进行 window.parent.parent 调用的隐藏 iframe。
I've done this two ways.
If the iframe is on the same domain as the parent website, you can just, in javascript, access window.parent.
If it isn't, however...I've done a dirty trick. I'll share it here, though, as it may help.
We created a page on the other domain, which would call to window.parent.parent. We put that in a hidden iframe inside the iframed page, and send it a querystring argument or two. It's not pretty, but it gets around cross-domain scripting problems.
This basically means that you have this sort of thing:
admin.example.com
content.example.com - iframe
admin.example.com?contentid=350 - hidden iframe that makes a window.parent.parent call.
整个练习的目的是对您的网站进行功能测试吗?如果是这样,请考虑使用 Selenium 来自动与网站交互,而不是自定义 Firefox 插件。它适用于所有主要浏览器,并支持检查您尝试执行的页面元素(使用 XPath)。它还具有一个名为 Selenium IDE 的 Firefox 插件,可让您方便地“记录”与稍后自动播放的网站。
Is the point of this whole exercise functional testing of your website? If so, instead of your custom Firefox plugin, consider using Selenium to automate interactions with websites. It works with all major browsers and supports the inspection of page elements you are trying to do (using XPath). It also features a Firefox plugin called Selenium IDE that allows you to conveniently "record" your interactions with a website for automated playback later.