本文介绍了如何使用 JS 延迟加载新的 Google Adsense 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌已经取代

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js</script>



ref:Google Adsense 公告

旧的 Adsense 代码如下:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:350px;height:90px"
     data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
     data-ad-slot="XXXXXXXXXX"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

新的 Adsense 代码如下:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:350px;height:90px"
     data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
     data-ad-slot="XXXXXXXXXX"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

页面加载完成后加载广告的旧 JS 代码是:

    <script type="text/javascript">
        function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
        document.body.appendChild(element);
        }
        if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
        else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
        else window.onload = downloadJSAtOnload;
    </script>

作为 ?client=ca-pub-xxxxxx"crossorigin="anonymous" 添加在新广告代码的脚本标签中,现在加载广告的新JS代码是什么?

As ?client=ca-pub-xxxxxx" crossorigin="anonymous" is added in script tag of new Ad Code, now what would be the new JS code to load the ads?

推荐答案

嗯,这不是真正的延迟加载 这是延迟加载,不推荐,但在这里

Well This is not really lazy load This is defer loading and not recommended, but here you go

<script>
function downloadJSAtOnload() {
    var element = document.createElement("script");
    element.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX";
    element.async = true;
    element.setAttribute('crossorigin', 'anonymous');
    document.body.appendChild(element);
}
if (window.addEventListener)
    window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
    window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>

如果您正在寻找延迟加载 AdSense,请查看 延迟加载Adsense

If you are looking for lazy load AdSense check out Lazy Loading Adsense

这篇关于如何使用 JS 延迟加载新的 Google Adsense 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 02:31