Skip to content

Instantly share code, notes, and snippets.

@sureshHARDIYA
Last active January 15, 2024 06:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sureshHARDIYA/c662dc6b3d66fca58d83fbbd4c8137f3 to your computer and use it in GitHub Desktop.
Save sureshHARDIYA/c662dc6b3d66fca58d83fbbd4c8137f3 to your computer and use it in GitHub Desktop.
Create Google Adsense ad using JavaScript
var adUnit = document.getElementsByClassName("section-below-title")[0];
var parentElement = adUnit.childNodes[0];
// Create script element
var script = document.createElement('script');
script.async = true;
script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
parentElement.appendChild(script);
// Create INS element
var ins = document.createElement('ins');
ins.className = "adsbygoogle";
ins.style = "display:inline-block;width:300px;height:250px;";
ins.setAttribute('data-ad-client', "ca-pub-XXXXXXXXXXXXXXXX");
ins.setAttribute('data-ad-slot', "XXXXXXXXXX");
parentElement.appendChild(ins);
(adsbygoogle = window.adsbygoogle || []).push({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment