Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load multiple javascript asynchronously? [duplicate]

Tags:

javascript

i have around 5 to 6 JavaScript which i put it at the bottom of page however I wanted to load all this JavaScript in one function so all of them download on the page Asynchronously. Can anyone suggest how can i do that? Some syntaxes will definitely helpful

`

like image 462
JqueryGuy Avatar asked Sep 14 '26 10:09

JqueryGuy


1 Answers

On newer browsers you can use the async attribute:

<scritp src="yourscript.js" type="text/javascript" async=true ></script>

Or for wider support you can append the script to an already processed tag in the document:

Edit:

function loadScript (scriptpath){
    var s=document.getElementsByTagName('script')[0];
    var ss=document.createElement('script');
    ss.type='text/javascript';
    ss.async=true;
    ss.src= scriptpath;
    s.parentNode.insertBefore(ss,s);
}

And you can load them like so:

loadScript('script1.js');
loadScript('script2.js');
loadScript('script3.js');
loadScript('script4.js');
like image 113
Ibu Avatar answered Sep 16 '26 22:09

Ibu