When it comes to path-related issues, everyone will go to the window.location and think that this object provides quite a lot of path information, which is commonly used to include:
Location.href: Full URL of the current page
Location.pathname: path name in current URL
Location.hash: Anchor points in the current URL
Location.search: Query parameters in the current URL
However, location does not have an attribute that directly obtains the absolute path of the current directory (without the filename). Through Google I found some wrong ways, such as the URL through "/" separated by the number of groups, the last item of the array is removed and then connected to a string. However, if a filename is not specified in the URL, the result is wrong.
Based on previous coding experience, the href attribute of a element always returns an absolute path, which means it has the ability to turn a relative path into an absolute path. Try it with the following code:
var a = document.createelement (' a ');
A.href = './';
alert (a.href);
A = null;
Unfortunately, this method is not valid under old IE 6/7, and when executing alert (A.HREF), the pop-up is still "./". Later, I found that some people in the StackOverflow also raised the problem, and the solution is very simple, as long as a through innerHTML injection can be:
var div = document.createelement (' div ');
div.innerhtml = ' <a href= './"></a>";
alert (div.firstChild.href);
div = null;
One might ask: why not use regular expressions? My answer is: to consider whether there is a file name, there is no anchor point, there is no query parameters, this regular expression may be quite complex.
Example
function Getrealpath () {//Get current URL, such as: http://localhost:8083/myproj/view/my.jsp var curwwwpath=window.document.loc
Ation.href;
Gets the directory after the host address, such as: myproj/view/my.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:/myproj var projectname=pathname.substring (0,pathname.substr (1). IndexOf ('/') +1);
http://localhost:8083/myproj var realpath=localhostpaht+projectname was obtained;
alert (Realpath); }
Example
Here's how JavaScript gets the path to the file:
function Servermappath (fileName) {
var syspath = location.href;
Syspath = Syspath.tolowercase (); Change the path name to lowercase
myposition = syspath.lastindexof ("/"); Gets the last "/"
Syspath = syspath.substring (0,parseint (myposition) +1) in the file path;//the String before "/" is intercepted using the SUBSTRING function, Gets the path to the current directory
Syspath = Syspath.replace ("
file:///
", ""); Here to
replace the file:///
is empty, otherwise it will be an error
Syspath = syspath.replace (New RegExp ("%20", "GM"), ""); If the file name contains spaces, then to restore the space, replace all%20 as ""
Syspath = syspath + filename;
return syspath.tostring ();
}