It mainly uses the Location object, which contains information about the current URL. It is a part of the Window object and can be accessed through the Window.location property.
Method 1, js method to obtain the project root path
function getRootPath(){ var curPageUrl = window.document.location.href; var rootPath = curPageUrl.split("//")[0] + curPageUrl.split("//")[1].split("/")[0] + curPageUrl.split("//")[1].split("/")[1]; return rootPath;}Method 2 (window.document.location.href/window.document.location.pathname) ------------------Reposted from the Internet
function getRootPath_web() { //Get the current URL, such as: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwPath = window.document.location.href; //Get the directory after the host address, such as: uimcardprj/share/meun.jsp var pathName = window.document.location.pathname; var pos = curWwwPath.indexOf(pathName); //Get the host address, such as: http://localhost:8083 var localhostPaht = curWwwPath.substring(0, pos); //Get the project name with "/", such as: /uimcardprj var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1); return (localhostPaht + projectName);}Method 3 (window.location.pathname/window.location.protocol/window.location.host)
function getRootPath_dc() { var pathName = window.location.pathname.substring(1); var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/')); if (webName == "") { return window.location.protocol + '//' + window.location.host; } else { return window.location.protocol + '//' + window.location.host + '/' + webName; }}Note:
1. Document implies a document object, while window implies a window object, and there can be multiple document objects under a window.
So there is only one window.location.href under a window, but there may be multiple document.URL, document.location.href--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. window.location.href and document.location.href can be assigned values and then jump to other pages. The document.URL can only be read but not written ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. For detailed information of Location object, refer to w3school //www.VeVB.COM/w3school/jsref/dom_obj_location.htm
Wulin.com editor added:
Exclude advertising implementations for certain directories
var pathName = window.document.location.pathname;var projectName = pathName.substring(1, pathName.substr(1).indexOf('/') + 1);var ad_projectlist = ',,web,html5,css,';if(ad_projectlist.indexOf(','+projectName+',') < 0){ alert("web,html5,css several directory codes are not executed");}The above is the entire content of this article. I hope you can give you a reference and I hope you can support Wulin.com more.