I have a js file that I want to load it dynamically in a web page using JavaScript. I want to load the file after the web page has been loaded.
Any help is welcome.
You can do this with pure javascript or jQuery, here is the two ways (I prefer the jQuery way):
JavaScript
var h = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.src = 'http://url.to/your/script.js';
h.appendChild(s);
jQuery
$.getScript('http://url.to/your/script.js');
window.onload = function () {
var head = document.getElementsByTagName('head').item(0);
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'some.js');
head.appendChild(script);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With