JS gets the current url host address project root path

  • 2020-03-29 23:50:38
  • OfStack

//Gets the current url, such as HTTP: __localhost: 8080 / Tmall/index. The JSP
var curWwwPath=window.document.location.href; 

//Get the host address after the directory such as: /Tmall/index.jsp
var pathName=window.document.location.pathname; 
var pos=curWwwPath.indexOf(pathName); 

//Gets the host address, such as: HTTP: arbitration localhost:8080
var localhostPaht=curWwwPath.substring(0,pos); 

//Get the project name with "/", such as: /Tmall
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); 

Related articles: