URL parsing and url Decoding
Refer to blog: [basic advanced] URL explanation and URL Encoding
I. URI vs URL
URI :( Uniform Resource Identifier unified Resource Identifier ).
URL: (Uniform/Universal Resource Locator unified Resource Locator ).
Link:
-
- URI is a lower-level URL abstraction, a string text standard.
- That is to say, the URI belongs to the parent class, And the URL belongs to the URI subclass. A URL is a subset of a URI.
- The difference between the two is that URI represents the path of the Request server and defines such a resource. The URL also describes how to access this resource (http ://).
Ii. URL
Protocol |
Access |
Used... |
Http |
Hypertext Transfer Protocol |
A common webpage starting with http. Not encrypted. |
Https |
Secure Hypertext Transfer Protocol |
Secure Web pages to encrypt all information exchanges. |
Ftp |
File Transfer Protocol |
Used to download or upload files to a website. |
File |
|
Files on your computer. |
(Host = hostname + port)
-
-
- Hostname: Domain Name
- Port: the default port of the HTTP server is 80 (can be omitted ). If another port is used, it must be specified, for example: http://www.cnblogs.com: 8080/
- Pathname: The Path to access the resource (including files)
- Url-params: included Parameters
- Search: data sent to the http server
- Hash: Anchor
3. Example:
Http://www.myw-ebsite.com/sj/test;id=8079? Name = sviergn & x = true # stuff
Schema: http
Host. domain: www.mywebsite.com
Path:/sj/test
URL partams: id = 8079
Query String: name = sviergn & x = true
Anchor: stuff
Iii. URL Parsing
Function parseURL (url) {// Method 1: dynamically create a tag (HTMLAnchorElement object) var a = document. createElement ('A');. href = url; // Method 2: dynamically create a URL object: var a = new URL (url); return {source: url, protocol:. protocol. replace (':', ''), hostname:. hostname, port:. port, pathname:. pathname, segments:. pathname. replace (/^ \//,''). split ('/'), // first remove/starting with pathname, and then split the remaining file according to/: (. pathname. match (/([^ /? #] +) $/I) | [, '']) [1], // if the end of the pathname contains no /? # Capture group, it is filename; otherwise, filename is an empty string search:. search, params: (function () {var ret = {}; var seg =. search. replace (/^ \? /, ''). Split ('&'); var len = seg. length; for (var I = 0; I <len; I ++) {if (! Seg [I]) {continue;} var s = seg [I]. split ('='); ret [s [0] = s [1];} return ret;}) (), hash:. hash. replace ('#','')};}
Usage:
var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');myURL.source; // 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'myURL.protocol; // 'http'myURL.hostname; // 'abc.com'myURL.port; // '8080'myURL.pathname; // '/dir/index.html'myURL.segments; // Array = ['dir', 'index.html']myURL.file; // 'index.html'myURL.search; // '?id=255&m=hello'myURL.params; // Object = { id: 255, m: hello }myURL.hash; // 'top'