异步加载外部applet的机制

时间:2017-03-03 18:07:08

标签: javascript html css

我在我的一个网站上有adsense,一个典型的页面可能在其html上包含这样的内容:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 2009Menu -->
<ins class="adsbygoogle"
     style="display:inline-block;width:120px;height:90px"
     data-ad-client="ca-pub-xxxxxx"
     data-ad-slot="xxxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>


<div align="center">

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 2009Vertical -->
<ins class="adsbygoogle"
     style="display:inline-block;width:160px;height:600px"
     data-ad-client="ca-pub-xxxxxxx"
     data-ad-slot="xxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

这件事使得页面需要花费大量时间来加载....通常为6秒。没有它,页面加载0.5秒!!!!!!!

理论上,此广告已经异步提供(哈!)

在加载所有内容之前,页面不会显示(!?)

是否有任何机制可以使用javascript,css或其他方式使此加载异步?

1 个答案:

答案 0 :(得分:1)

而不是async使用defer。使用defer标记脚本在页面加载之前不会运行! 像这样:

<script src="source/to/script" defer></script>