Speeding Up Your Website by Lazy Loading AdSense Ads

User Author

Achilles.H

August 02, 2021

Speeding Up Your Website by Lazy Loading AdSense Ads

Are you eager to speed up your own website to get more traffic and sales? Maybe you could try lazy loading AdSense ads or other HTML elements to meet your need. The details about how to do so are shown below in this article: Speeding Up Your Website by Lazy Loading Adsense Ads.

How to speed up your website?

If a bigger JScript needs to be loaded before the Onload of a page, the page loading speed will be slower.

Loading some JScript codes that are not related to the page display 1 to 2 seconds after the page's Onload, such as statistics code and Adsense advertising code, which is known as lazy loading, can speed up the website and improve page loading and display speed.

Web page loading speed can be tested by Google Pagesspeed insights: https://developers.google.com/speed/pagespeed/insights.

The following JScript code allows Adsense ads to load Adsense code with a 0.5 second delay after Onload.

Tips: If you set a long dalay time, your adsense accout may be suspended for invalid traffics. See my experience Lazy Loading AdSense Ads could Lead to Invalid Traffics.

        <script type="text/javascript">
         window.onload = function(){setTimeout("loadAds()", 500); 
         } 
         
        function loadAds(){
           loadScript('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',function(){
               console.log("loaded script.")}); 
        }  
             
        function loadScript(src, callback) {      
        var script = document.createElement('script');          
        var head = document.getElementsByTagName('head')[0];      
        script.type = 'text/javascript';      
        script.charset = 'UTF-8';      
        script.src = src;      
        if (script.addEventListener) {          
            script.addEventListener('load', function() { callback();}, false);      
        } else if (script.attachEvent) {
            script.attachEvent('onreadystatechange', function () {  
                var target = window.event.srcElement;  
                if (target.readyState == 'loaded') { 
                callback();
                }  
            });      
        }      
        head.appendChild(script); 
        }
        </script>