純js自動批量引入js、css插件,支持自定義參數


 
         
 //autoload.js
 1 ;! function(e) {
 2     var autoload = e.autoload || {};
 3     e.autoload = autoload;
 4     e.autoload = new function() {
 5         this.options = {
 6             id: 'autoload',
 7         }
 8         var o = this.options;
 9         this.include = function(_opt) {
10             o = _opt;
11             var id = document.getElementById('autoload');
12             if(typeof id!=='undefined'){
13                 var cssid = id.getAttribute('css');
14                 var jsid = id.getAttribute('js');
15             }
16             cssid = autoload.split(cssid);
17             jsid = autoload.split(jsid);
18             var c = 0;    //記錄js的起始位置
19             for(var i = 0; i < o.path.length; i++) {
20                 var file = o.path[i];
21                 
22                 if (file.match(/.*.js$/)){
23                     var ind = i+1-c;    //記錄js的起始位置
24                     if(jsid.toString().indexOf(ind)>-1){ //判斷id中的js鏈接是否存在
25                         document.write('<script type="text/javascript" src="' + file + '"></script>');
26                     }
27                 }else if (file.match(/.*.css$/)){
28                     c++;
29                     if(cssid.toString().indexOf(i+1)>-1){
30                         var Link = document.createElement('link');
31                         Link.href = o.path[i];
32                         Link.rel = 'stylesheet';
33                         document.getElementsByTagName('head')[0].appendChild(Link);
34                     }
35                 }
36             }
37         }
38     }
39     autoload.split = function(str){
40         var strs= new Array(); //定義一數組 
41         strs=str.split(","); //字符分割 
42         return strs;
43     }
44 }(window)
45 
46 //調用
47 autoload.include({
48     id: 'autoload'    //引入的該js的id
49     //引入的路徑
50     ,path: [
51         'css/a.css'
52         ,'css/b.css'
53         ,'js/1.js'
54         ,'js/2.js'
55         ,'js/3.js'
56         ,'js/4.js'
57         ,'js/5.js'
58     ]
59 });
</body>前引入,js和css為path中參數的位置,例中js位置為1,2,4從1開始查找,css同理。
1 <script src="js/autoload.js" id="autoload" js="1,2,4" css="1,2"></script>

 

 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM