Created
January 12, 2017 21:22
-
-
Save klihelp/499028199f852f407c5f54cb0f8f38ba to your computer and use it in GitHub Desktop.
Load JS files dynamically from innerHTML
This file contains hidden or 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
/** | |
* | |
* @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