jq给css、js自动加版本号

646 阅读1分钟
//  给页面引用的css和js加上版本号
function resource_loader(config) {  
    this.css = config.css;  
    this.scripts = config.scripts;  
    this.head = document.getElementsByTagName('head')[0];  
    //版本号采用时间戳 
    this.v = '?v=' + new Date().getTime();  
    this.load = function() {      
        this.loadCSS();      
        this.loadScript();  
    }  

 //加载js引用  
    this.loadScript = function() {      
        var that = this;      
        this.scripts.forEach(function(scriptlink) {          
        var script = document.createElement("script");          
        script.type = "text/javascript";          
        script.src = scriptlink + this.v;          
        this.head.appendChild(script);      
        });  
    }  
    this.load();  
    return  resource_loader
 }

//html引用方法
<script>          
// 动态给css js 加版本号       
 $(function() {          
    resource_loader({             
     css: [
        'layui/adminIndex.css'
     ],              
    scripts: [                 
     'layui/adminIndex.js'             
     ]          
    });     
 })  
</script>