A small and fast JavaScript plugin that loads Google AdSense only when the user scrolls or clicks.
This helps your website load faster and improves Core Web Vitals.
- Loads AdSense only when user interacts
- Helps with Core Web Vitals and speed
- Optional Publisher ID support
- Uses localStorage to avoid reloading
- No dependencies, just 2KB
- Works on all browsers
Use this link from jsDelivr:
<script>
window.lazyAdClient = "ca-pub-1234567890123456"; // Replace with your ID
</script>
<script src="https://cdn.jsdelivr.net/gh/mukeshkushwahae/lazy-adsense@main/dist/lazy-adsense.min.js" defer></script>Add this before in your HTML file.
- Download or clone the repo:
git clone https://github.com/mukeshkushwahae/lazy-adsense.git- Copy the file:
dist/lazy-adsense.min.js
- Include it in your page:
<script>
window.lazyAdClient = "ca-pub-1234567890123456"; // Replace with your ID
</script>
<script src="your-path/lazy-adsense.min.js" defer></script>- When the user scrolls or clicks, AdSense is loaded.
- A value is saved in localStorage so it only loads once.
- If a window.lazyAdClient is set, it adds the client=ca-pub-XXXX to the AdSense script.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lazy AdSense</title>
</head>
<body>
<script>
window.lazyAdClient = "ca-pub-1234567890123456"; // Replace with your ID
</script>
<script src="https://cdn.jsdelivr.net/gh/mukeshkushwahae/lazy-adsense@main/dist/lazy-adsense.min.js" defer></script>
</body>
</html>MIT License © Mukesh Kushwaha