I work with a certain CMS (MediaWiki) in which there is no direct control over HTML (as there is in Drupal Twig files for example);
Hence, in this case, I have to load my Adsense verification code via JavaScript.
The following code pattern should work (Babelified to ES3, as in MediaWiki 1.33.0 one cannot run ES6):

var head = document.querySelector("head");
var addToHead = [`
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    (adsbygoogle = window.adsbygoogle || []).push({
        google_ad_client: "ca-pub-VALUE",
        enable_page_level_ads: true

head.innerHTML += addToHead;

Should there be any (long term) problem with this approach?

  • 1
    I mean, is this currently working for you? Do you not see a problem with it? Because it seems to me like you're overwriting all the contents in the head, and replacing it with just this script. Doesn't this destroy all your other scripts, and CSS styles? Sep 10, 2019 at 9:17
  • @DigitalNinja I don't recall anything like that; I think I was confused with the PHP code of the ads themselves and the above code didn't even run because it's ES6 and MediaWiki 1.33.0 can only run JS up to ES3
    – user58733
    Sep 10, 2019 at 21:33
  • I should check again in a few minutes
    – user58733
    Sep 10, 2019 at 21:33
  • @DigitalNinja I edited the question - I don't have time to learn how to use Babelify now and indeed ES6 doesn't run at all in MediaWiki 1.33.0 so for now I should just keep this question open.
    – user58733
    Sep 10, 2019 at 22:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.