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:
1.location.href: Full URL of the current page
2.location.pathname: path name in current URL
3.location.hash: Anchor points in the current URL
4.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:
Copy Code code as follows:
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:
Copy Code code as follows:
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.