- JS get the current domain name there are two ways
Method one: var domain =document.domain;
Method two: var domain =window.location.host;
Note the problem:
Since the current domain name obtained does not include http://, so when assigning the acquired domain name to the A-tag href, do not forget to add http://, otherwise the navigation error when clicking the link.
- 4 ways to get the current URL
- method to get the current URL parameter
Mode one: varURL =window.location.href;
Mode two: varURL =self.location.href;
Mode three: varURL = document. URL;
Mode four: varURL =document.location;
Note the question: What is displayed in the IE Address bar and what URL is obtained.
3. Ways to get relative paths
First get the URL, then the URL through//cut into two parts, and then intercept the relative path from the latter part. If there are parameters in the intercepted relative path, the parameters are removed.
Functiongeturlrelativepath ()
{
varURL =document.location.tostring ();
Vararrurl = Url.split ("//");
var start = Arrurl[1].indexof ("/");
Varrelurl = arrurl[1].substring (start),//stop omitted, intercepts all characters from start to end
if (Relurl.indexof ("?")! =-1) {
Relurl= relurl.split ("?") [0];
}
Returnrelurl;
}
Calling method: Geturlrelativepath ();
Example: If the current Url is http//www. Liangshunet. COM/PUB/ITEM.ASPX?T=OSW7, the relative path that is intercepted is:/pub/item.aspx.
Functiongeturlpara ()
{
varURL =document.location.tostring ();
Vararrurl = Url.split ("?");
Varpara = arrurl[1];
Returnpara;
}
Calling method: Geturlpara ()
Example: If the current Url is http//www. Liangshunet. COM/PUB/ITEM.ASPX?T=OSW7, the part of the parameter that is intercepted is: T=OSW7.
- Gets the method that specifies the URL parameter
Paraname wait for the name of the parameter
Functiongeturlparam (paraname) {
varURL =document.location.tostring ();
Vararrobj = Url.split ("?");
if (arrobj.length> 1) {
Vararrpara = Arrobj[1].split ("&");
Vararr;
for (var i = 0; I <arrPara.length; i++) {
Arr= arrpara[i].split ("=");
if (arr! = Null&&arr[0] ==paraname) {
RETURNARR[1];
}
}
Return "";
}
else {
Return "";
}
}
Calling Method: Geturlparam ("id");
To illustrate:
If the URL of the webpage has such a parameter test.htm?id=896&s=q&p=5, then call Geturlparam ("P"), return 5.
JS gets the current domain name, URL, relative path and parameters, and specified parameters