如何在iFrame中触发iFrame加载
How to trigger iFrame load within an iFrame?
我向iFrame发送消息,但仅在iFrame加载事件时发送。在更改DOM后,希望在不重新加载页面的情况下手动触发它。
说明:这是一种调查,在页面更改时我发送页面大小。当我在DOM中插入新图像时,会出现问题,我需要告诉父页面并触发iFrame load
事件。仅在iframe加载时,消息会转到parent->iframe,而不是iframe->parent。
与其触发加载事件,不如使用IFrame发布的消息。
外部框架中的JavaScript(这会为消息附加一个事件侦听器,并在收到消息时显示警报):
window.addEventListener('message', function(e) {
// This check can be removed for testing purposes, but for security
// it's strongly recommended that you leave it in there and replace
// the domain with the one of your IFrame's src, to ensure you process
// only messages coming from your own code and not somebody else's.
if(e.origin != "http://yourdomain.com") return;
alert("Got a message: " + e.data);
});
内部框架中的JavaScript(这会向父窗口发送一条消息——它应该会触发那里的侦听器并显示警报):
// The second parameter can be replaced by "*" for testing, but again it
// is strongly recommended to use the domain you expect the outer frame
// to have, to ensure your message lands in the right window (in case
// another page loaded yours in an IFrame).
window.parent.postMessage("Hello World!", "http://yourdomain.com");
您也可以传递JS对象,而不是"Hello World!"
。
它也以另一种方式工作:如果您在IFrame中也安装了一个消息侦听器,那么您也可以使用document.getElementById('myIframe').contentWindow.postMessage(...)
之类的东西将消息从外部框架发送到内部框架。
另请参阅:http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage
相关文章:
- 如何在iFrame中触发iFrame加载
- 在iframe加载后删除类
- BrowserAction.onClicked等待iframe加载
- Iframe加载调整大小在Chrome中工作,但不能在IE或Firefox中工作
- Dojo:在 iframe 加载时解析 HTML 内容
- Iframe 加载回调被多次调用
- 阻止iframe加载到移动设备上
- 如何将隐藏/显示事件延迟到提交后Iframe加载
- Iframe加载动画不起作用
- iframe 加载事件中的 setTimeout 似乎没有像预期的那样等待 x 秒
- 强制同步 iframe 加载
- 如何javascript调用iframe加载的文件函数
- 显示微调器,直到 iframe 加载 http post 响应
- 在 KnockoutJS 应用程序中将 AngularJS 应用程序作为 iFrame 加载
- 如何捕获 iframe 加载错误
- 在执行函数之前等待多个 iFrame 加载
- JavaScript 在 Iframe 加载时显示加载 gif
- 如何使 iFrame 在 iframe 加载特定页面时重新加载它所在的页面
- 要随索引页一起加载的滚动更新,并在 iframe 加载来自链接的内容时消失
- 一个页面上有多个 Iframe 加载问题