Home  >  Article  >  Web Front-end  >  JS gets the current URL and host address project root path_javascript skills

JS gets the current URL and host address project root path_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:101266browse

Copy code The code is as follows:

//Get the current URL, such as: http:// localhost:8080/Tmall/index.jsp
var curWwwPath=window.document.location.href;

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

//Get the host address, such as: http://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);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn