The main use of the Location object, containing information about the current URL, is a part of the Window object that can be accessed through the Window.location property.
Method One (Window.document.location.href/window.document.location.pathname)------------transferred from Network
function Getrootpath_web () {
//get current URL, such as: Http://localhost:8083/uimcardprj/share/meun.jsp
var curwwwpath = Window.document.location.href;
Gets the directory after the host address, such as: Uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curwwwpath.indexof (pathName);
Get 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 Two (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, the document implied is a Documentation object, window is implied by a Windows object, a window can have more than one document object.
So there is only one window.location.href under a window, but there may be multiple document.url, Document.location.href------------from the network
2, Window.location.href and document.location.href can be assigned value, and then jump to other pages, Document.url can only read or write------------transferred from the network
3, Location object detail information Reference W3school http://www.jb51.net/w3school/jsref/dom_obj_location.htm
Small series of cloud-dwelling communities supplement:
Exclude 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 do not execute");
}
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.