在写纯Html网站的时候,每个页面的头部菜单、js、css和底部说明都是同样的,有的时候你要改,就要一个一个的去改,通过下面方法将这些相同的抽取出来,方便后期维护!
希望能帮到你~!
在applyBusiness.html页面引入公共页头header.html。
1、applyBusiness.html:
我的申请任务科信业务事项:
故障申报服务申请
状态:
全部
待提交
办理中
已办结
业务来源:
申请时间:
-
搜索
重置
2、header.js:
/**
* 引用JS和CSS头文件
*/
var rootPath = getRootPath(); //项目路径
/**
* 动态加载CSS和JS文件
*/
var dynamicLoading = {
meta : function(){
document.write('');
},
css: function(path){
if(!path || path.length === 0){
throw new Error('argument "path" is required!');
}
document.write('');
},
js: function(path, charset){
if(!path || path.length === 0){
throw new Error('argument "path" is required!');
}
document.write('');
}
};
/**
* 取得项目路径
* @author wul
*/
function getRootPath() {
//取得当前URL
var path = window.document.location.href;
//取得主机地址后的目录
var pathName = window.document.location.pathname;
var post = path.indexOf(pathName);
//取得主机地址
var hostPath = path.substring(0, post);
//取得项目名
var name = pathName.substring(0, pathName.substr(1).indexOf("/") + 1);
return hostPath + name + "/";
}
//动态生成meta
dynamicLoading.meta();
//动态加载项目 JS文件
dynamicLoading.js(rootPath + "/js/common/jquery-1.9.1.min.js", "utf-8");
dynamicLoading.js(rootPath + "/js/common/bootstrap.min.js", "utf-8");
dynamicLoading.js(rootPath + "/js/process/center/common/baseApp.js", "utf-8");
dynamicLoading.js(rootPath + "/js/process/center/common/bootstrap-datetimepicker.min.js", "utf-8");
dynamicLoading.js(rootPath + "/js/process/center/common/bootstrap-datetimepicker.zh-CN.js", "utf-8");
dynamicLoading.js(rootPath + "/js/process/center/common/jquery.dataTables.js", "utf-8");
dynamicLoading.js(rootPath + "/js/platform/system/loadHeader.js", "utf-8");
//动态加载项目 CSS文件
dynamicLoading.css(rootPath + "/css/common/bootstrap-3.3.5/css/bootstrap.min.css");
dynamicLoading.css(rootPath + "/css/common/bootstrap-3.3.5/css/bootstrap-responsive.css");
dynamicLoading.css(rootPath + "/css/workflow/css/jquery.dataTables.css");
dynamicLoading.css(rootPath + "/css/workflow/css/newWorkflow.css");
3、loadHeader.js
$(function(){
$(".container").append('
$("#header").load(getRootPath() + "header.html");
});
4、header.html