这几天我把网站接入了谷歌广告,能赚点零花钱是其次,空荡荡的网站我觉得适当加点广告更能凸显美感,自制的横幅广告太无趣了。
起初想接入国内的广告商,但是想了想国内的广告商广告内容太差劲了,令人作呕的内容 都有,于是乎就想到了谷歌广告,接入谷歌广告的弊端就是谷歌因为退出中国,在中国访问速度实在是太慢了,导致整站界面加载完后还在加载谷歌广告,接着就找到方法,试了试还挺不错,记录一下。
谷歌广告代码默认每一段都需要加载JS文件,我们简化一下把JS放到网站头部统一加载
<!---GOOgle AdSense---> <script>window.onload = function() { setTimeout(function() { let script = document.createElement("script"); script.setAttribute("async", ""); script.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; document.body.appendChild(script); }, 2e3);}</script>
把上面这段代码放到网站头部 head 标签内
然后把你网站谷歌广告代码的加载JS文件部分去掉
谷歌广告默认的代码是这样的
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- ad --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pb-424931094073" data-ad-slot="2366870"></ins> <script>
我们把引入JS部分去掉然后放到你的网站即可
<!-- ad --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pb-4249310980254073" data-ad-slot="236601"></ins> <script>