在jsp中可以使用<%=path%> 获取当前项目名,但如果在外部js中则不能使用。
以下是替代方案:
//获取当前网址,如:
var currentWPath=window.document.location.href;
//获取主机地址之后的目录如:/project/index.jsp
var pathName=window.document.location.pathname;
var pos=currentWPath.indexOf(pathName);
//获取主机地址,如://localhost:8080
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/project
var projectName=pathName.substring(0,pathName.substr(1).indexOf(’/’)+1);