JS gets the current domain name, URL, relative path and parameters, and specified parameters

Source: Internet
Author: User

    1. 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.
    1. 4 ways to get the current URL
    2. 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.

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.