javascript - 我想按顺序执行该功能
问题描述
一个.html
var iframe = document.querySelector('#test');
// iframe.src = 'B.html'
iframe.onload = window(){
testFunction();
}
B.html
window.onload = window(){
console.log('B.html');
}
我想要“console.log('B.html') -> TestFunction();”
但是 TestFunction(); 总是先执行。(B.html onload 不起作用。)帮助我...
解决方案
您需要您的 iframe 以某种方式与父窗口通信,以表明它已被加载。看看跨文档消息传递。由于跨域策略,我无法在此处放置工作片段,但如果您拥有自己的 Web 服务器,这样的事情应该可以工作:
<iframe id='myFrame' src='B.html' onLoad='onFrameLoad()'></iframe>
// A.html
function receiveMessage(event) {
console.log('A.html', event);
}
function onFrameLoad(){
console.log('B.html');
window.parent.postMessage('loaded');
}
window.addEventListener('message', receiveMessage, false);
推荐阅读
- colors - TABLEAU 中不同行的颜色标记
- c++ - 从具有共同祖先的模板类指向派生类的指针容器作为模板参数
- javascript - 使用 vanilla javascript 从 XHR 请求中附加 HTML
- java - AWS Step Function 中的 OutPutPath 中的多个 JSON 路径
- javascript - 状态改变后反应不重新渲染
- python - 使用 Keras 的二元分类总是给出错误的预测:acc 总是 0.5
- ruby - 通过 docker-compose 中的 Entrypoint 命令与 Ruby Padrino 控制台交互
- javascript - 通过鼠标悬停使用 setTimeout 停止幻灯片,通过鼠标悬停开始
- javascript - 普通函数和自调用函数有什么区别-JS
- react-native - 在 React Native 中单击 TouchableOpacity 时如何创建模式?