Javascript can be used to obtain the current domain name, Url, relative path and parameters separately. The so-called separate attack, that is, the domain name does not include the path and parameters of the web page file, the parameters do not include the domain name and the web file path, which are respectively introduced below.
First, there are 2 ways for js to get the current domain name.
1, method one
var domain = document.domain;
2, method two
var domain = window.location.host;
3, pay attention to the problem
Since the current domain name obtained does not include http://, when assigning the obtained domain name to the href of the a tag, don't forget to add http://, otherwise the navigation will be wrong when you click the link.
Second, the four methods to get the current Url
var url = window.location.href;
var url = self.location.href;
var url = document.URL;
var url = document.location;
Ie What is displayed in the address bar, what is the obtained url.
Third, the method of obtaining the current relative path
First get the Url, then cut the Url into two parts by //, 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 omits, intercepts all characters from start to end
If (relUrl.indexOf( "?" ) != -1){
relUrl = relUrl.split( "?" )[0];
}
return relUrl;
}
Call method: GetUrlRelativePath();
For example: If the current Url is http// www.infinetsoft.com/pub/item.aspx?t=osw7, the relative path intercepted is: /pub/item.aspx.
Fourth, the method of obtaining the current Url parameter
1, get the Url parameter part
function GetUrlPara ()
{
var URL = document.location.toString ();
var arrUrl = url.split ( "?" );
var para = arrUrl[1];
return para;
}
Call method: GetUrlPara()
For example: If the current Url is http// www.infinetsoft.com/pub/item.aspx?t=osw7, the intercepted parameter part is: t=osw7.
2. For the value of the specified parameter in the Url, please see the article " Js Get the specified Url parameter ".
Post your comments / questions
Recent Article
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
- Unicode error 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
- Could not find the 'angular-devkit/build-angular:dev-server' builder's node package | Angular Error
- Error: error:0308010C:digital envelope routines::unsupported
Related Article