小编d.a*_*iel的帖子

iframe问题需要减少页面加载时间

我有一个网站(wordpress),上面有几千个帖子.帖子看起来像这样:

[tab:source1]
<iframe width="560" height="315" src="https://www.youtube.com/embed/U3rKPexoEcA" frameborder="0" allowfullscreen></iframe>
[tab:source2]
<iframe src="https://player.vimeo.com/video/60718161?title=0&byline=0&portrait=0&badge=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p><a href="https://vimeo.com/60718161">HOW TO SHARPEN PENCILS</a> from <a href="https://vimeo.com/pricefilms">Pricefilms</a> on <a href="https://vimeo.com">Vimeo</a>.</p>
[tab:source3]
<iframe width="640" height="360" src="http://www.worldstarhiphop.com/embed/100588" frameborder="0" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)

我的页面加载时间有时是两位数.我理解为什么会这样,但我不确定如何解决它.我想用以下两种方法修复它:

Iframe setTimeout()
Run Code Online (Sandbox Code Playgroud)

或者:动态Asynch Iframe看起来像这样:

<script>
(function(d){
  var iframe = d.body.appendChild(d.createElement('iframe')),
  doc = iframe.contentWindow.document;

  // style the iframe with some CSS
  iframe.style.cssText = "position:absolute;width:200px;height:100px;left:0px;";

  doc.open().write('<body onload="' + 
  'var d = document;d.getElementsByTagName(\'head\')[0].' + 
  'appendChild(d.createElement(\'script\')).src' + 
  '=\'\/path\/to\/file\'">');

  doc.close(); //iframe onload event happens

  })(document);
</script>
Run Code Online (Sandbox Code Playgroud)

使用这两种方法,我如何全局地将其应用于整个网站的所有iframe?我认为不可能单独更改每个iframe.

html javascript php pageload page-load-time

11
推荐指数
1
解决办法
202
查看次数

标签 统计

html ×1

javascript ×1

page-load-time ×1

pageload ×1

php ×1