You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

If you need to load javascript by the case like domain name, following code solve your pain point.


Example 1) Loading script by the screen width

var head = document.getElementsByTagName('head')[0];
var js = document.createElement("script");

js.type = "text/javascript";

if (screen.width > 500)
{
    js.src = "js/jquery_computer.js";
}
else
{
    js.src = "js/mobile_version.js";
}

head.appendChild(js);


Example 2) Loading Google Adsense script by the domain name

<!-- Global site tag (gtag.js) - Google Analytics -->
<div id="adsense_space"></div>
<script>
  var head = document.getElementsByTagName('adsense_space')[0];

  var sUA = "your default User Agent ID";
  if (document.domain.toLowerCase()=="enewtown.com") sUA="UA-1093021-17"; else sUA="UA-1093021-16";


  var js = document.createElement("script");
  js.type = "text/javascript";
  js.src = "https://www.googletagmanager.com/gtag/js?id=" + sUA;


  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', sUA);
</script>
  • No labels