- function getContextPath () {
- // 获取当前网址,如:http://localhost:8080/ssm/index.jsp
- var currentPath = window.document.location.href;
- // 获取主机地址之后的目录,如: /ssm/index.jsp
- var pathName = window.document.location.pathname;
- var pos = currentPath.indexOf(pathName);
- // 获取主机地址,如: http://localhost:8080
- var localhostPath = currentPath.substring(0, pos);
- // 获取带"/"的项目名,如:/ssm
- var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
- alert("currentPath:" + currentPath + "\\n"
- + "pathName:" + pathName + "\\n"
- + "localhostPath:" + localhostPath + "\\n"
- + "projectName:" + projectName + "\\n"
- + "contextPath:"+ localhostPath + projectName);
- return(localhostPath + projectName);
- }
- //该片段来自于http://www.codesnippet.cn/detail/2007201513166.html
来源: http://www.codesnippet.cn/detail/2007201513166.html