-
-
Save jpsantos7/06a64d5cb99bb84bc47109efe7fa5eff to your computer and use it in GitHub Desktop.
Conditionally load script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const loadScript = (src) => { | |
let s = document.createElement('script'); | |
s.src = src + "?" + new Date().getTime(); // add timestamp or not | |
s.type = "text/javascript"; | |
s.async = false; // or true | |
// s.addAttribute('myAttribute', 'myAttributeValue'); // if needed | |
document.getElementsByTagName('head')[0].appendChild(s); // or before closing the <body> | |
} | |
// Usage - either put this inside a script on the <head> or <footer> and conditionally load it as follows | |
// if (document.domain == 'myDomain.com') { | |
// loadScript('https://myawesomesite/myscriptfile.js'); | |
//} | |
export default loadScript; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment