JS Gets the example of the domain name, URL, relative path, and parameter in the URL link

Source: Internet
Author: User


Use JS to extract the domain name (domains) in the URL, with the split () function on it.

Because a correct URL must be http://or https://, domain, path/parameter composition, so you can use split to/to divide the group, take the 3rd part is the domain name.

code example:

var url = ' http://www.111cn.net/phper/php.html ';
var domain = url.split ('/'); Split with "/"
if (Domain[2]) {
domain = domain[2];
} else {
domain = '; If the URL is not correct, take empty
}

Supplemental: Current domain name, URL, relative path, and parameter


One, JS get the current domain name there are 2 ways

1. Method One

var domain = Document.domain;

2, Method two

var domain = Window.location.host;

3, pay attention to the problem

Because the current domain name does not include http://, the acquired domain name is assigned to the href of a label, so don't forget to add http://, otherwise the navigation will go wrong when you click 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;

What does the IE Address bar show, and what is the URL to get?

How to get the current relative path

First get the URL, and then cut the URL through//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, intercepting 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. Liangshunet. COM/PUB/ITEM.ASPX?T=OSW7, the relative path that is intercepted is:/pub/item.aspx.

Iv. methods for obtaining the current URL parameters

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. Liangshunet. COM/PUB/ITEM.ASPX?T=OSW7, the part of the parameter that is intercepted is: T=OSW7.


To get the implementation of a specified parameter in a URL directly

First through the document.location to obtain the current access to the Web site, followed by the split method through "? "Divide the Web site into two parts. If there are parameters in the URL (arrobj.length > 1); Then use the split method to separate each parameter by "&", then use the For loop to check if there is the same parameter in the parameter as the parameter to be found, and if so, return the value of the parameter; Continue looping until all parameters are found. Returns null if there are no parameters in the URL and no parameters are found.

2, the implementation code is as follows:

Paraname and so on to find 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 "";
}
}

Call Method: Geturlparam ("id");

An example is provided:

If the URL of the Web page has such a parameter test.htm?id=896&s=q&p=5, then call Geturlparam ("P") and return 5.

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.