A comprehensive javascript URL parsing function and segmented URL Parsing Method

Source: Internet
Author: User

I. URL parsing functions
Copy codeThe Code is as follows:
<Script>
/**
* @ Param {string} complete url address
* @ Returns {object} custom object
* @ Description usage example: var myURL = parseURL ('HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top ');

Myurl.file+'index.html'

MyURL. hash = 'top'

MyURL. host = 'abc. com'

MyURL. query = '? Id = 255 & m = hello'

MyURL. params = Object = {id: 255, m: hello}

MyURL. path = '/dir/index.html'

MyURL. segments = Array = ['dir', 'index.html ']

MyURL. port = '123'

MyURL. protocol = 'http'

MyURL. source = 'HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top'

*/
Function parseURL (url ){
Var a = document. createElement ('A ');
A. href = url;
Return {
Source: url,
Protocol: a. protocol. replace (':',''),
Host: a. hostname,
Port: a. port,
Query: a. search,
Params: (function (){
Var ret = {},
Seg = a. search. replace (/^ \? /, ''). Split ('&'),
Len = seg. length, I = 0, s;
For (; I <len; I ++ ){
If (! Seg [I]) {continue ;}
S = seg [I]. split ('= ');
Ret [s [0] = s [1];
}
Return ret;
})(),
File: (a. pathname. match (// ([^ \/? #] +) $/I) | [, '']) [1],
Hash: a. hash. replace ('#',''),
Path: a. pathname. replace (/^ ([^ \/])/, '/$1 '),
Relative: (a. href. match (/tps? : \/[^ \/] + (. +)/) | [, '']) [1],
Segments: a. pathname. replace (/^ \ //, ''). split ('/')
};
}

// Var myURL = parseURL ('HTTP: // abc.com: 8080/dir/index.html? Id = 255 & m = hello # top ');
Var myURL = parseURL ('HTTP: // localhost: 8080/test/mytest/toLogina. ction? M = 123 & pid = abc ');
Alert (myURL. path );
Alert (myURL. params. m );
Alert (myURL. params. pid );
</Script>

Ii. JS segmented URL Parsing

URL: Uniform Resource Locator (Uniform Resource Locator, URL)
The complete URL consists of the following parts: scheme: // host: port/path? Query # fragment
Copy codeThe Code is as follows:
Scheme = communication protocol (Common http, ftp, maito, etc)
Host = host (domain name or IP address)
Port = port number
Path = path
Query = query (optional) used for dynamic web pages (such as CGI, ISAPI, PHP/JSP/ASP. NET and other technologies) transmission parameters can have multiple parameters, separated by the "&" symbol, the names and values of each parameter are separated by the "=" symbol .)
Fragment = Information fragment (string, used to specify fragments in network resources. For example, if a webpage contains multiple glossary, you can use fragment to directly locate the glossary. (Also called anchor .))

For such a URL
Http://www.jb51.net: 80/seo /? Ver = 1.0 & id = 6 # imhere

We can use javascript to obtain all the parts.
1, window. location. href
The whole URl string (the complete address bar in the browser)

2, window. location. protocol
URL protocol section
Returned value in this example: http:

3, window. location. host
Host part of the URL
Returned value in this example: www.jb51.net

4, window. location. port
URL Port
If the default port 80 is used (update: Even if port 80 is added), the return value is not the default port 80, but an empty character.
Returned value in this example :""

5, window. location. pathname
URL path (that is, the file address)
Returned value in this example:/seo/

6. window. location. search
Query (parameter)
In addition to assigning values to dynamic languages, we can also assign values to static pages and use javascript to obtain expected parameter values.
Return Value in this example :? Ver = 1.0 & id = 6

7. window. location. hash
Anchor
Returned value in this example: # imhere

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.