Lazy loading is a technique that loads ads only when they're about to come into view. This method is particularly useful for websites with a lot of content or infinite scrolling.
Pros:
Cons:
Client-side loading involves loading ads directly onto the user's browser using JavaScript. This method allows for more control over ad placement and can improve ad visibility.
Pros:
Cons:
Lazy loading means you delay loading the AdSense script and ad unit until the user actually scrolls near the ad’s position.
How it works: You use Intersection Observer or a library to detect when the ad container enters the viewport. Only then do you inject the AdSense script.
Basic lazy load example (using Intersection Observer): adsense loading method
document.addEventListener("DOMContentLoaded", function() { const adContainers = document.querySelectorAll('.lazy-adsense');const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { // Load AdSense script and push the ad let script = document.createElement('script'); script.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; script.async = true; document.head.appendChild(script);
(adsbygoogle = window.adsbygoogle || []).push({}); observer.unobserve(entry.target); } });});
adContainers.forEach(container => observer.observe(container)); });
Pros:
Cons:
Verdict: Perfect for long-form content, infinite scroll pages, and ad units at the very bottom of the page. Do not lazy load your first ad unit above the fold.
let observer = new IntersectionObserver((entries) =>
entries.forEach(entry =>
if (entry.isIntersecting)
// Load AdSense ad here
observer.unobserve(entry.target);
);
);
observer.observe(adContainer);