Javascript URI parsing introduction

  • 2020-05-16 06:20:39
  • OfStack

Parsing URI is an interesting thing that I didn't realize it could be so complicated.

URI

The wikipedia explanation for URI looks like this:


  In computer terms, a 1 Resource identifier ( Uniform Resource Identifier Or, URI) is 1 One is used to identify something 1 A string of Internet resource names. This identity allows the user to access 1 World wide web (WWW) resources interact through specific protocols. URI Defined by a schema that includes the determination of the syntax and associated protocols.

This is from a web explanation of the composition of URI, which can be seen later in the parsing of URI.

URI1 is composed of three parts:

1. Naming mechanism for accessing resources.
2. Host name of the resource.
3. The name of the resource itself, represented by the path.

Or you could say it looks like this, they look like they're going to be 1.

The URL format consists of the following three parts:

1. Protocol (or service mode)
2. The IP address of the host where the resource is stored (sometimes including the port number)
3. The specific address of the host resource. , such as directories and file names

URI parsing


  "Resolution" 1 a URI Would mean 1 A relatively URI The reference is converted to absolute form or retrieved by attempt 1 Be solved in a lead URI or 1 a URI Reference the represented resource to de-reference this URI . The "parsing" part of document processing software usually provides both.

Javascript URI parsing

Simply take the example of search JS in a blog, here is its URL,

//www.ofstack.com/search/?q=js&type=
And then there is


 var parser = document.createElement('a');
 parser.href = "//www.ofstack.com/search/?q=js&type="

We can then know its protocol, port number, host, specific address, and so on

 parser.protocol;
 parser.host;
 parser.pathname;
 parser.search;

The result is

 protocol:http
 host:www.ofstack.com
 pathname:/search/
 search:?q=js&type=

The result above adds up to a complete URI. It's just that this part of parser.search is not very well understood, for ? For the number, it should be the parameter, the parameter for the search.

If it is for URI, which is only 1 mail, let's say URI is


 mailto:h@ofstack.com?subject=hello
 

then

 var parser = document.createElement('a');
 parser.href = "mailto:h@ofstack.com?subject=hello";  > parser.protocol
 "mailto:"
 > parser.pathname
 "h@ofstack.com"
 > parser.search
 "?subject=hello"
 


Related articles: