参考地址:http://www.shangxueba.com/jingyan/1901890.html
代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
//获取完整路径 为: http://localhost:6981/InspectionData/TEST.html
var fullPath=window.document.location.href;
//获取主机地址之后的路径 为: /InspectionData/TEST.html
var pathName=window.document.location.pathname;
//获取主机地址之后的路径长度 为: /InspectionData/TEST.html的长度为25
var pos=fullPath.indexOf(pathName);
//获取主机地址 为: http://localhost:6981
var localhostPath=fullPath.substring(0,pos);
//获取项目名 为: /TEST.html
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
alert("fullPath为:\n"+fullPath+"\npathName为:\n"+pathName+"\npos为:\n"+pos+"\nlocalhostPath为:\n"+localhostPath+"\nprojectName为:\n"+projectName);
</script>
</body>
</html>
在本机服务器上执行结果: