从 iframe 访问父窗口(跨域) [英] Access parent window from iframe (cross-domain)

查看:33
本文介绍了从 iframe 访问父窗口(跨域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 iFrame 中的窗口是从另一个域加载的,我遇到了从 iFrame 访问父窗口的任务.如果我理解正确,所有现代浏览器现在都允许这样做.所以我来这里是为了寻找最佳解决方案.

我接下来要完成这个:

我有一个带有 iFrame 的叠加层.这将代替弹出窗口起作用,以防止弹出窗口阻止程序阻止我的内容.任务是在 iFrame 中的文档完成一些工作后重新加载主页.在将加载到 iFrame 的文档中,我将添加

在父窗口中,我将添加函数,该函数将每秒调用一次,并检查此 div 是否仍具有类名false".当这将更改为true"时,我将调用一些回调.

如果您有更好的解决方案,请与我分享.将不胜感激任何帮助.

编辑:这是不可能的,因为不仅不可能从子窗口操作父窗口,反之亦然.我的想法是从父窗口操作子窗口.我错了.

解决方案

如果我是你,我会查看 window.postMessage.它可能会做你想做的:

参考如下:

I've encountered the task to access parent window from iFrame, if the window in iFrame was loaded from another domain. If I understand correctly, all modern browsers do now allow to do this. So I'm here to find the best solution.

I'm going to accomplish this next way:

I have an overlay with an iFrame within it. This will work instead of pop-ups to prevent pop-up blockers to block my content. The task is to reload the main page when the document in the iFrame finishes some work. In a document, which will be loaded to an iFrame, I will add

<div id="is_closed" class="false"></div>

In a parent window I'll add function, which will be called every second and check if this div still has class name "false". When this will be changed to "true", I'll call some callbacks.

If you have any better solution, please share it with me. Will appreciate any help.

Edit: This is impossible because it's not only impossible to manipulate parent window from child window, but vice versa too. My idea was to manipulate child window from parent window. I was wrong.

解决方案

If I were you I would check out window.postMessage. It may do what you want:

For reference see the following:

这篇关于从 iframe 访问父窗口(跨域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆