URL parsing and url Decoding

Source: Internet
Author: User

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'

 

 

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.