Excerpt from: https://www.cnblogs.com/wangdahai/p/6221399.html
have been modified.
First, JS get the current domain name of 2 ways
Method One
var domain = Document.domain;
Method Two
var domain = Window.location.host;
Note: Because the current domain name obtained does not include/HTTP, so when assigning the acquired domain name to the an href of the a tag, do not forget to add the http://, otherwise the navigation error when clicking the link.
Ii. 4 ways to get the current URL
var url = window.location.href;
var url = self.location.href;
var url = document. URL;
var url = document.location;
Note: What the Address bar shows is what the URL gets to.
Third, the method of obtaining the current relative path
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.
function Geturlrelativepath ()
{
var url = document.location.toString ();
var arrurl = Url.split ("//");
var start = Arrurl[1].indexof ("/");
var relurl = arrurl[1].substring (start),//stop omitted, intercepts all characters from start to end
if (Relurl.indexof ("?")! =-1) {
Relurl = Relurl.split ("?") [0];
}
return relurl;
}
Calling method: Geturlrelativepath ();
Example: If the current URL is http//www.liangshunet.com/pub/item.aspx?t=osw7, then the relative path to intercept is:/pub/item.aspx.
Iv. methods for obtaining the current URL parameters
1. Get the URL Parameters section
function Geturlpara ()
{
var url = document.location.toString ();
var arrurl = Url.split ("?");
var para = arrurl[1];
return para;
}
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.
V. Methods for obtaining the specified URL parameters
Paraname wait for the name of the parameter
function Geturlparam (paraname) {
var url = document.location.toString ();
var arrobj = Url.split ("?");
if (Arrobj.length > 1) {
var Arrpara = Arrobj[1].split ("&");
var arr;
for (var i = 0; i < arrpara.length; i++) {
arr = arrpara[i].split ("=");
if (arr! = null && arr[0] = = Paraname) {
return arr[1];
}
}
Return "";
}
else {
Return "";
}
}
Calling Method: Geturlparam ("id");
Example: If the URL of the page 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