Other

Load Javascript source Dynamically


function loadJavaScriptOnDemand(src, callback) {
var script = document.createElement('script');
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);

script.onreadystatechange = function() {
if (script.readyState == 4 || script.readyState == "complete") {

if (typeof callback == "function") {
callback();
}
callback = null;
}
}
}

Above function you can call as following.

loadJavaScriptOnDemand(‘path/to/your/javascript’,function(){alert(‘JavaScript loaded’)});

Beto López
Software engineer & Full stack website developer who has left a startup job where wednesdays were "yoga days", to build an standarized-low-cost technical support service, deepest than than the service hosting companies provide, for website owners, freelancers and digital businesses. Personal Website, Linkedin y Twitter.


Load Javascript source Dynamically
We are always online, we reply fast
Get in touch
Php Ninja