han*_*chi 5 html javascript postmessage
如你所知,使用html5的API postMessage,我们可以将消息发布到当前页面的iframe,或者到一个新的弹出窗口,但是如果我们像这样编码:
var popwindow = window.open('http://localhost:8080/index2.html');
popwindow.postMessage({
"age": 10
},
'http://localhost:8080/index2.html');
Run Code Online (Sandbox Code Playgroud)
当弹出窗口尚未加载时,我们将无法收到消息,因为我们使用“postMessage”,那么我们如何确保弹出窗口已加载?popwindow.onload我们不能在当前页面使用,那怎么办呢?
你总是可以使用
window.opener.postMessage(...
Run Code Online (Sandbox Code Playgroud)
在index2.html中,向开启者发出它已加载的信号
或者,有一种老派的方式:
在index.html中
function imOpen(win) {
win.postMessage(// whatever ...
}
window.open('index2.html');
Run Code Online (Sandbox Code Playgroud)
在index2.html中
window.addEventListener('load', function() {
window.opener.imOpen(window);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9786 次 |
| 最近记录: |