从 iFrame 中触发 javascript 事件
我不确定这是否可行,但我有一个小型网络应用程序负责将文件上传到服务器。此应用程序由另一个应用程序使用,该应用程序将其包含在 iFrame 中。
上传器应用程序使用 jQuery 表单在后台提交文件,我希望能够在上传完成并触发成功回调时通知父应用程序。
我正在考虑以某种方式将一个函数传递到 iFrame 应用程序中,该函数可以从父级调用,但我不确定如何做到这一点,或者是否可能。
I am not sure if this is possible but I have a small web application that is responsible for uploading files to the server. This app is used by another app which includes it in an iFrame.
The uploader app is using jQuery form to submit the file in the background and I would like to be able to notify the parent app when the upload is complete and the success callback fires.
I was thinking about somehow passing a function into the iFrame app that could be called from the parent but I am not sure how to do this or if it is even possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果两个应用程序托管在同一域中,则完成此操作很简单。
从 iframe 调用父窗口中的函数:
从父窗口调用 iframe 中的函数:(
这要求 iframe 设置
name
属性)。如果应用程序位于不同的域中,您将必须执行一些更大的黑客才能完成此操作。
If both apps are hosted on the same domain, accomplishing this is trivial.
To call a function in the parent window from the iframe:
To call a function in the iframe from the parent window:
(This requires that the iframe have a
name
attribute set).If the apps are on different domains, you'll have to perform some greater hackery to accomplish this.
您可以调用parent.functionname。或者您可以尝试 IFrame 事件 onLoad 从父页面本身调用。
You can call parent.functionname. Or you can try the IFrame event, onLoad to call from the parent page itself.
我不确定执行此操作的正确方法是什么,但您可以在父框架中编写类似
document.callback = function() { ... }
的内容,然后iframe 中的parent.document.callback()
。当然,父框架/iframe 中的页面必须来自同一域,否则浏览器将阻止它们之间的任何交互。
I'm not sure what's the right way to do this, but you can write something like
document.callback = function() { ... }
in your parent frame andparent.document.callback()
in your iframe.Of course your pages in parent frame/iframe must be from the same domain or browser will block any interaction between them.