Lazyload and Defer Adsense 2021 with Crossorigin Attribute

Recently Adsense changed back the 2021 Adsense JS script code by adding the crossorigin attribute. Some Company friends asked me to update the Adsense lazyload script and Adsense defer to match the latest changes to the Adsense script code.

Crossorigin Attribute

But before that, let's get to know a little about the crossorigin attribute. This crossorigin attribute is applied to resources loaded or requested from other domains outside our own domain which is of course for security.

By crossorigin="anonymous"then this tells the browser that these resources can be requested anonymously without sending credentials such as cookies, certificates or HTTP Basic authentication that is safe to use by anyone.

Here is the latest 2021 Adsense script code:
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxx" crossorigin="anonymous"></script>

For that, please use the codes below for the latest 2021 Adsense lazyload and defer.
Lazyload and Defer Adsense 2021 with Crossorigin Attribute
Lazyload Latest Adsense 2021

Please use the following code and save it above </body>
<script>
//<![CDATA[
var lazyadsense = false;
window.addEventListener("scroll", function(){
if ((document.documentElement.scrollTop != 0 && lazyadsense === false) || (document.body.scrollTop != 0 && lazyadsense === false)) {
(function() { var ad = document.createElement('script'); ad.setAttribute('crossorigin','anonymous'); ad.async = true; ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxx'; var sc = document.getElementsByTagName('script')[0]; sc.parentNode.insertBefore(ad, sc); })();
lazyadsense = true;
  }
}, true);
//]]>
</script>

For the marked code, please adjust it with the publisher ID code.

Latest Adsense Defer 2021

Please use the following code and save it above </body>
<script>
//<![CDATA[
function downloadJSAtOnload(){var d=document.createElement("script");d.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxx";d.setAttribute("crossorigin","anonymous"),document.body.appendChild(d)}window.addEventListener?window.addEventListener("load",downloadJSAtOnload,!1):window.attachEvent?window.attachEvent("onload",downloadJSAtOnload):window.onload=downloadJSAtOnload;
//]]>
</script>

For the marked code, please adjust it with the publisher ID code.

Then to defer Adsense please add Adsense JS preload below <head>
<link as='script' crossorigin='anonymous' href='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxx' rel='preload'/>

For the marked code, please adjust it with the publisher ID code.

Hope it is useful.
Previous Post Next Post