【问题标题】:I want to perform the function in order我想按顺序执行该功能
【发布时间】:2019-04-13 20:39:19
【问题描述】:

A.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 不起作用。) 帮帮我……

【问题讨论】:

  • 为什么不在B.html 中调用testFunction();
  • 我希望 B.html onload 先执行,A.html 执行
  • 这两个文件是怎么加载的?

标签: javascript html jsp web iframe


【解决方案1】:

您需要您的 iframe 以某种方式与父窗口通信,以表明它已被加载。看看cross-document messaging。 由于跨域策略,我无法在此处设置有效的 sn-p,但如果您有自己的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多