Latest Lazyload And Defer Adsense With Crossorigin

Table of Contents
Latest Lazyload And Defer Adsense With Crossorigin

In the following article, I will explain a little about the crossorigin attribute in Adsense JS code. This crossorigin attribute is applied to resources loaded from other domains outside our own domain which of course is for security purposes.

Using this attribute crossorigin="anonymous" tells the browser that the resource can be requested anonymously without sending credentials such as cookies, certificates, or basic HTTP authentication so that it is safe for anyone to use.

Here is the latest Adsense script code.

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxx" crossorigin="anonymous"></script>

You can use the codes below for the latest Lazyload and Defer Adsense. Use the following code and save above the code </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>

Change the marked code and adjust it to your publisher ID.

Latest Adsense Defer

  • Use the code below and save 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>

Change the marked code and adjust it to your publisher ID.

  • Next is for Adsense defer
  • Add the Adsense Javascript preload below the code <head>

<link as='script' crossorigin='anonymous' href='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxx' rel='preload'/>

Change the marked code and adjust it to your publisher ID.

ANDD
ANDD Daily workers

2 comments

Comment Author Avatar
December 18, 2021 Delete
Sangat berguna..
Comment Author Avatar
October 02, 2022 Delete
Terimakasih