在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ iframe 中異步加載的 script 會阻塞 frame 的 load 事件

iframe 中異步加載的 script 會阻塞 frame 的 load 事件嗎?

使用 iframe 打開一個子頁面,子頁面中有一段異步加載 script 文件的代碼,如:

var node = doc.createElement('script');
node.src = 'xxx.js';
node.async = true;
head = doc.getElementsByTagName('head')[0];
head.appendChild(node);
node.addEventListener('load', function(e){
    console.log('script complete!!');
}, false);

父頁面給 iframe 綁定了 load 事件:

$('iframe').on('load', function(){
    console.log('frame complete!!');
});

請問 iframe 的 load 事件會等待 這個異步的 js 加載完畢再觸發(fā)嗎?

回答
編輯回答
編輯回答
愿如初

onload就是當(dāng)前iframe或document加載完成后的回調(diào)??梢杂米鞅O(jiān)聽iframe加載

2017年12月10日 19:28
編輯回答
冷咖啡

做了個測試

index.html

(function () {
  var iframe = document.createElement('iframe')

  iframe.src = './test.html'
  iframe.style.display = 'none'

  document.body.appendChild(iframe)

  iframe.onload = function () {
    console.log('iframe loaded!')
  }
}());

test.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" async="true" onload="console.log('async script loaded!')"></script>
  </head>
  <body>

  </body>
</html>

clipboard.png

看起來結(jié)果是先加載異步腳本,后執(zhí)行 iframe load 事件

2017年2月8日 02:00