Seach

dynamic javascript loader

this script aim is to load js files dynamically by passing the location as an array and dynamically include it to dom
load.html
<html>
<head>
<script type="text/javascript" src="load.js" >

</script>
</head>
<body>
<script type="text/javascript" >
options=["module","module1"];//scripts to be loaded
obj.create(options);//calling the creater function
</script>
</body>
</html>
load.js(created using modular design pattern)
var obj=(function()
{

var obj=
{};
this.create=function(script)
{
//script is an array containing the names of the scripts need to be included
for(i=0;i<script.length;i++)
{
js=script[i]+".js";
element=document.createElement('script');//creating dom element script
element.setAttribute('type','text/javascript');//setting script attributes
    element.setAttribute('src',js);
    document.getElementsByTagName('head')[0].appendChild(element);//appending it to head
  }
 
}
return this;

}());