Skip to content

Instantly share code, notes, and snippets.

@klihelp
Created January 12, 2017 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klihelp/499028199f852f407c5f54cb0f8f38ba to your computer and use it in GitHub Desktop.
Save klihelp/499028199f852f407c5f54cb0f8f38ba to your computer and use it in GitHub Desktop.
Load JS files dynamically from innerHTML
/**
*
* @type is typescript
* @td use better logic for every content load
*/
var loadJsURL = function(url) {
var canJsLoad = function(url) {
if (!url) return false;
var scripts = document.getElementsByTagName('script');
for (var i = scripts.length; i--;) {
// *td
// better with substring or pos, thinking of // start
if (scripts[i].src == url) return false;
}
return true;
}
// Load js url
var insertJsUrl = function(url) {
var script = document.createElement('script');
script.setAttribute('src', url);
document.body.appendChild(script);
}
if ( canJsLoad(url) ) {
insertJsUrl(url)
}
}
// example
// loadJsURL('www.website.com/embed.js')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment