Skip to content

Instantly share code, notes, and snippets.

@jpsantos7
Last active May 5, 2022 13:13
Show Gist options
  • Save jpsantos7/06a64d5cb99bb84bc47109efe7fa5eff to your computer and use it in GitHub Desktop.
Save jpsantos7/06a64d5cb99bb84bc47109efe7fa5eff to your computer and use it in GitHub Desktop.
Conditionally load script
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