JS gets the information of the various parts of the URL

Source: Internet
Author: User

URL: Uniform Resource Locator (Uniform Resource Locator, URL)
The complete URL consists of the following parts:
Scheme: Communication protocol
Common Http,ftp,maito, etc.

Host: Hosts
The server (computer) domain Name System (DNS) hostname or IP address.

Port: Port number
An integer, optionally, that omits the default port for using the scenario, such as HTTP, with the default port of 80.

Path: Paths
A string separated by 0 or more "/" symbols, typically used to represent a directory or file address on a host.

Query: Querying
Optional, for use with dynamic Web pages such as CGI, ISAPI, php/jsp/asp/asp. NET and other technical Web pages) pass parameters, can have multiple parameters, separated by the "&" symbol, the name and value of each parameter is separated by the "=" symbol.

Fragment: Pieces of information
String that specifies the fragment in the network resource. For example, there are multiple noun interpretations in a Web page, and you can use fragment to navigate directly to a noun interpretation. (also known as anchor points.)

For such a URL


We can use JavaScript to get every part of it.
1, Window.location.href
The entire URL string (in the browser is the full address bar)
This example returns the value: Http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

The protocol portion of the URL
This example returns the value: http:

The host part of the URL
This example returns the value: Www.x2y2.com

The port portion of the URL
If you are using the default 80 port (update: Even if you added: 80), the return value is not the default of 80 but the null character
This example returns the value: ""

The path portion of the URL (that is, the file address)
This example returns the value:/fisker/post/0703/window.location.html

Query (Parameters) Section
In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the values of the arguments we believe in.
This example returns the value:? ver=1.0&id=6

Anchor Point
This example returns the value: #imhere then you can use this method to take the specified parameter: functionhooyesquerystring (querystringname) {var returnvalue= ""; varurlstring=new String (document.location); var serachlocation=-1; Varquerystringlength=querystringname.length; do {serachlocation=urlstring.indexof (querystringname+ "\="), if (serachlocation!=-1) {if (Urlstring.charat ( serachLocation-1) = = '? ') || (Urlstring.charat (serachLocation-1) = = ' & ')) {urlstring=urlstring.substr (serachlocation); break;} Urlstring=urlstring.substr (serachlocation+querystringlength+1); }}while (Serachlocation!=-1) if (serachlocation!=-1) {varseperatorlocation=urlstring.indexof ("&"); Seperatorlocation==-1) {returnvalue=urlstring.substr (querystringlength+1);} else {returnvalue=urlstring.substring (querystringlength+1,seperatorlocation); }} return returnvalue;} Then: hooyesquerystring ("id") takes the value of the ID parameter

JS gets the information of the various parts of the URL

Related Article

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.