URL and URI
Many people will confuse these two nouns.
URL: (uniform/universal Resource Locator abbreviation, Uniform Resource Locator).
URI: (Uniform Resource Identifier abbreviation, Uniform Resource Identifier) ( represents a standard ).
Relationship:
A URI is a higher-level abstraction of a URL, a string literal standard.
That is, the URI belongs to the parent class, and the URL belongs to the subclass of the URI. A URL is a subset of the URI.
The difference is that the URI represents the path to the requesting server and defines such a resource. The URL also shows how to access the resource (http://).
Port and URL standard format
What is a port? Port, which is the equivalent of a data transmission channel. Used to accept certain data and then transmit it to the appropriate service, and the computer will then send the corresponding reply to the other party via the open end.
The role of the port: because the relationship between the IP address and the network service is a one-to-many relationship. So actually on the Internet is the IP address plus the port number to distinguish between different services.
Ports are marked by port numbers, with only integers, ranging from 0 to 65535.
URL standard Format
In general, the common definition format for URLs that we are familiar with is:
scheme://host[:p ort#]/path/.../[;url-params][?query-string][#anchor]
// we are familiar with the HTTP, HTTPS, FTP and the famous ed2k, Thunderbolt Thunder and so on. the IP address of the host//HTTP server or the default port for the domain name port#//HTTP server is 80, which can be omitted. If you are using a different port, you must indicate that the default port for Tomcat, for example, isthe path of 8080 http://localhost:8080/path // Access resource url-params //query-string // data sent to the HTTP server // Anchor Point positioning
Automatically parse URLs with a tag
A common scenario in development is the need to extract some required elements from the URL, such as host, request parameters, and so on.
The usual practice is to write the regular to match the corresponding fields, but this reference to James's blog, the principle is to create a dynamic a tag, using some of the browser's native methods and some of the regular (for the robustness of the regular or want), the perfect URL, to get any part we want.
The code is as follows:
//This function creates a new anchor element and uses location//properties (inherent) to get the desired URL data. Some String//operations is used (to normalize results across browsers).functionparseurl (URL) {varA = document.createelement (' A '); A.href=URL; return{source:url, Protocol:a.protocol.replace (‘:‘,‘‘), Host:a.hostname, Port:a.port, Query:a.search, params: (function(){ varRET ={}, 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]; } returnret; }) (), 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 ('/') };}
Usage Method:
varMyurl = 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;//= ' 8080 'Myurl.protocol;//= ' http 'Myurl.source;//= ' http://abc.com:8080/dir/index.html?id=255
Using the above method, any part of the URL can be parsed.
Reference: HTTPS://GITHUB.COM/CHOKCOCO/CNBLOGSARTICLE/ISSUES/6
URI and URL differences