Recently, I need to use js to initiate a request to the server. When using ajax, I need to fill in the url of the request. I found the method of js to obtain the current project path on the Internet, and write it down for future reference:
function getRootPath(){
//当前页面
var curTotalPath=window.document.location.href;
//当前页面路径
var pathName=window.document.location.pathname;
//当前页面路径起始位置
var pathNamePos=curTotalPath.indexOf(pathName);
//host
var hostPath=curTotalPath.substring(0,pathNamePos);
//工程名
var projectName=pathName.substr(0,pathName.substr(1).indexOf('/')+1);
return hostPath+projectName;
}
The result is entered below: