来自另一个域的 iFrame 如何摆脱自身?
我正在为各种网站构建一个书签,其功能基本上如下:
- 用户导航到 foo.com(不在我控制范围内的外部网站)
- 用户单击浏览器上的书签,该书签从 foo.com 获取一些信息并将其显示在表单上在 iframe 内的 bar.com
- 用户在 foo.com 内的 bar.com 内提交表单
- iframe 消失
问题是让 iFrame 自行删除。由于我无法访问另一个域中父域内的函数,因此我无法创建自定义函数parent.removeIFrame(),甚至无法执行window.top.location.reload()。我尝试了很多技术,但由于跨域脚本,所有技术都失败了。
我解决这个问题的方法是将 top.location.href 更改为我在 bar.com/back.html 构建的自定义页面,其中它所做的只是发送一个“history.back()”命令来反弹原始页面上的用户。虽然很有创意,但我认为这不是一个非常优雅的解决方案。有更好的办法吗?
I am building a bookmarklet for various sites that functions basically like this:
- User navigates to foo.com (an external site not on my control)
- User clicks a bookmarklet on his browsers that grabs some information from foo.com and displays it on a form in bar.com inside an iframe
- User submits the form inside bar.com which is inside foo.com
- The iframe dissapears
The problem is getting the iFrame to remove itself. Since I can't access a function inside a parent domain in another domain I cannot create a custom function parent.removeIFrame() or even do a window.top.location.reload(). I tried many techniques but all of them fail because of cross domain scriptings.
The way I solved it was to change the top.location.href to a custom page I built at bar.com/back.html in which all it does is simply to send a single "history.back()" command that bounces the user on the original page. Althought creative, I don't think it's very elegant solution. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试跨域消息传递的片段标识符。有一个 YUI 库: http://www.julienlecomte.net/blog/ 2007/11/31/
You can try fragment identifier for cross domain messaging. There is a YUI library for it: http://www.julienlecomte.net/blog/2007/11/31/
当您注入 JS 代码以在网站页面上创建表单时,您可以让它轮询(新创建的 iframe 之外的 js)您的服务器以了解何时删除 iframe。您可以使用各种方法来执行此操作,包括 jsonp、长轮询、发布消息、片段标识符等。您不需要为此加载大型库 - 小书签越小越好。
When you injected the JS code to create the form on the site's page you can have it poll (js outside of the newly created iframe) your server to know when to remove the iframe. You can use all kinds of methods of doing this including jsonp, long polling, post message, fragment identifiers, etc. You do not need to load a big library for this - the smaller the better for bookmarklets.