If you need to load javascript by the case like domain name, you can do that by getElementByTagName() and appendChild(). For instance, you can run application by the screen width.
Example 1) Loading script by the screen width
function addJavascript(jsname) { var th = document.getElementsByTagName('head')[0]; var s = document.createElement('script'); s.setAttribute('type','text/javascript'); s.setAttribute('src',jsname); th.appendChild(s); } if (screen.width > 500) { addJavascript("your_domain/js/desktop_version.js"); } else { addJavascript("your_domain/js/mobile_version.js"); }
Example 2) Loading Google Adsense script by the domain name
This is what actually I use for confluence having multiple space in different domain name.
<!-- Global site tag (gtag.js) - Google Analytics --> <script> function addJavascript(jsname) { var th = document.getElementsByTagName('head')[0]; var s = document.createElement('script'); s.setAttribute('type','text/javascript'); s.setAttribute('src',jsname); th.appendChild(s); } var sUA = "your default User Agent ID"; if (document.domain.toLowerCase()=="enewtown.com") sUA="UA-******-17"; else sUA="UA-*******-16"; addJavascript( "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>