JavaScript Parse URL method detailed _javascript tips

Source: Internet
Author: User

URL: Uniform Resource Locator (uniform Resource Locator, URL)

The complete URL is composed of these parts:
Scheme://host:port/path?query#fragment

Scheme = Communication Protocol (commonly used Http,ftp,maito, etc.)
Host = hosts (domain name or IP)
Port = port number
Path = paths

query = Queries
Optional for dynamic Web pages, such as using CGI, ISAPI, php/jsp/asp/asp. NET, and other technology-made pages, can have multiple parameters, separated by the "&" symbol, and the name and value of each parameter are separated by the "=" symbol.

fragment = pieces of information
A string that specifies the fragment in the network resource. For example, a Web page has multiple noun interpretations, which can be directly positioned to a noun interpretation using fragment. (also known as anchor points.)

For such a URL
Http://www.master8.net:80/seo/?ver=1.0&id=6

We can use JavaScript to get every part of it.
1, Window.location.href
Entire URL string (in the browser is the complete address bar)

2,window.location.protocol
The protocol part of the URL
This example returns the value: http:

3,window.location.host
Host part of URL
This example returns a value of: www.master8.net

4,window.location.port
Port portion of URL
If the default 80 port (update: Even if added: 80), then the return value is not the default of 80 but null characters
This example returns the value: ""

5,window.location.pathname
The path portion of the URL (that is, the file address)
This example returns a value of:/seo/

6,window.location.search
Query (Parameters) Section
In addition to assigning values to dynamic languages, we can also give static pages and use JavaScript to get the value of a parameter that is believed to be
This example returns a value:? ver=1.0&id=6

7,window.location.hash
Anchor Point
This example returns a value: #imhere

8. URL parameter value

Method One: Regular Analysis method

Copy Code code as follows:

function getquerystring (name) {
var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I");
var r = window.location.search.substr (1). Match (REG);
if (r!= null) return unescape (r[2]); return null;
}

Method Two: Split the group by splitting

Copy Code code as follows:

function Getrequest () {
var url = location.search; Get the "?" in the URL String after the character
var therequest = new Object ();
if (Url.indexof ("?")!=-1) {
var str = URL.SUBSTR (1);
STRs = Str.split ("&");
for (var i = 0; i < strs.length i + +) {
Therequest[strs[i].split ("=") [0]]=unescape (Strs[i].split ("=") [1]);
}
}
return therequest;
}

The method is very simple, but very practical, here are listed 2 of their own common methods, small partners have different methods also please tell, this article continues to update. Everybody progress together

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.