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 use if else statement in c++?
- How to use godaddy domain name with another godaddy hosting account?
- Restore of database 'DATABASE' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
- How to programmatically modifying the AppSetting value in web.config using c#?
- TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
- How to calculate the age from jQuery ui datepicker using c#?
- How to calculate days difference between two dates in c#?
- Changing date format in jQuery ui datepicker
Related Article