前一段时间公司要求为了方便代码管理与整洁降低多余代码,优化等,下达了一个需求:那就是统一加载css与js
代码如下,可以直接保存为html文件运行:
<script>
var top_url='${ctxsta}';
(function() {
///document.write('');
//动态加载css
function dynamicLoadCss(url) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
head.appendChild(link);
}
var cssFiles = [
top_url+"/css/system/SysUserList.css",
top_url+"/static/h-ui/css/H-ui.min.css",
top_url+"/static/h-ui.admin/css/H-ui.admin.css",
top_url+"/lib/Hui-iconfont/1.0.8/iconfont.css",
top_url+"/lib/jquery.easyui/default/easyui.css?t=<%= System.currentTimeMillis()%>",
top_url+"/lib/jquery.easyui/icon.css?t=<%= System.currentTimeMillis()%>",
top_url+"/static/h-ui.admin/skin/black1/skin.css?t=<%= System.currentTimeMillis()%>",
top_url+"/static/h-ui.admin/css/style.css?t=<%= System.currentTimeMillis()%>",
top_url+"/css/app.css?t=<%= System.currentTimeMillis()%>",
top_url+"/lib/chosen.jquery/chosen.css",
top_url+"/lib/resizableColumns/jquery.resizableColumns.css",
top_url+"/lib/jqgrid/css/ui.jqgrid.css",
top_url+"/lib/jqgrid/css/css/redmond/jquery-ui-1.8.16.custom.css",
top_url+"/lib/jqgrid/css/extjqgrid.css",
top_url+"/lib/layuiFoot/dist/css/layui.css",
];
var cssTags = new Array(cssFiles.length);
for (var k = 0, len = cssTags.length; k < len; k++) {
dynamicLoadCss(cssFiles[k]);
}
// 加载所有js文件
function dynamicLoadJs(url) {
var scri ='<script src='+url+' type="text/javascript"><'+'/'+'script>';
//console.log(scri);return;
document.write(scri);
}
var jsFiles = [
top_url+"/lib/jquery/1.9.1/jquery.min.js",
top_url+"/lib/layer/layer.js",
top_url+"/static/h-ui/js/H-ui.js",
top_url+"/static/h-ui.admin/js/H-ui.admin.js",
top_url+"/lib/chosen.jquery/chosen.jquery.js",
top_url+"/lib/resizableColumns/jquery.resizableColumns.min.js",
top_url+"/lib/avalon/dist/avalon.js",
top_url+"/js/tool.js?t=<%= System.currentTimeMillis()%>",
top_url+"/js/component.js?t=<%= System.currentTimeMillis()%>",
top_url+"/js/filters.js?t=<%= System.currentTimeMillis()%>",
top_url+"/js/app.js?t=<%= System.currentTimeMillis()%>",
top_url+"/js/app_basic_data.js?t=<%= System.currentTimeMillis()%>",
top_url+"/js/dwrRateUtil.js?t=<%= System.currentTimeMillis()%>",
top_url+"/lib/jquery.easyui/jquery.easyui.min.js",
top_url+"/lib/jquery.easyui/easyui-lang-zh_CN.js",
top_url+"/lib/jqgrid/js/i18n/grid.locale-cn.js",
top_url+"/lib/jqgrid/js/jquery.jqGrid.src.js",
top_url+"/js/jqGridModel.js",
top_url+"/lib/layuiFoot/dist/layui.js",
top_url+"/lib/layui/common.js",
top_url+"/lib/jquery.validation/1.14.0/jquery.validate.js",
top_url+"/lib/jquery.validation/1.14.0/validate-methods.js",
top_url+"/lib/jquery.validation/1.14.0/messages_zh.js",
top_url+"/lib/jquery.form/jquery.form.js",
top_url+"/js/gvalidate.js",
top_url+"/js/verify.js",
];
var scriptTags = new Array(jsFiles.length);
for (var i = 0, len = jsFiles.length; i < len; i++) {
dynamicLoadJs(jsFiles[i]);
}
})();
</script>
统一加载css与js
最新推荐文章于 2024-07-09 16:18:55 发布