1. location. href: complete url of the current page
2. location. pathname: The path name in the current url
3. location. hash: the anchor in the current url
4. location. search: query parameters in the current url
However, location does not have an attribute to directly obtain the absolute path of the current directory (excluding the file name. I found some incorrect methods through google. For example, the url is separated into an array by "/", and the last entry of the array is removed and then connected to a string. However, if the file name is not specified in the url, the result is very wrong.
Based on past coding experience, the href attribute of element a always returns an absolute path, that is, it has the ability to convert the relative path to an absolute path. I tried the following code and it turned out to be:
Vara = document. createelement ('A ');
A. href = './';
Alert (a. href );
A = null;
Unfortunately, this method is invalid in the old ie 6/7. When you execute alert (a. href), the pop-up is still "./". Later, I found someone raised this issue in stackoverflow, and the solution is very simple. Just inject a through innerhtml:
The code is as follows:
Vardiv = document. createelement ('div ');
Div. innerhtml = '<ahref =./> </a>;
Alert (div. firstchild. href );
Div = null;
Some may ask: why not use a regular expression? My answer is: this regular expression may be quite complicated to consider whether there is a file name, an anchor, or a query parameter.