/**
* 给页面引用的css和js加上版本号
* @param {Object} config 配置
*/
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();
}
//加载css引用
this.loadCSS = function() {
var that = this;
this.css.forEach(function(csslink) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = csslink + this.v;
this.head.appendChild(link);
});
}
//加载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();
}
调用方法:
<script type="text/javascript">
$(function() {
resource_loader({
css: [
'content/styles/common_page.css'
],
scripts: [
'http://res.wx.qq.com/open/js/jweixin-1.4.0.js',
'content/scripts/utils/wx_config.js'
]
});
})
</script>