+34 649 382 965 · 4.7 / 5 Read our Google reviews · contacto@phpninja.info
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
Full stack website developer focused in bug fixing and maintenance of Wordpress, Prestashop, HTML, CSS, Javascript, Php y Mysql. Open source contributor too. Beto Ayesa, Linkedin y Twitter.


Load Javascript source Dynamically
Te respondemos rapidamente
Get in touch