Home >Web Front-end >JS Tutorial >Introduction to Javascript URI parsing_javascript skills

Introduction to Javascript URI parsing_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:09:292041browse

Parsing URI is an interesting thing. I didn’t realize before that it can be so complicated.

URI

The explanation of URI in Wikipedia is as follows:

Copy code The code is as follows:

In computer terms, a Uniform Resource Identifier (URI) is a string used to identify the name of an Internet resource. This type of identification allows users to interact with resources on the network (generally referred to as the World Wide Web) through specific protocols. A URI is defined by a scheme that determines syntax and associated protocols.

Quoted from the explanation of URI composition on the Internet, and these can be seen in the analysis of URI later.

URI generally consists of three parts:

1. Naming mechanism for accessing resources.
2. The host name where the resources are stored.
3. The name of the resource itself, represented by the path.

Or it can be said that the two seem to be consistent.

The format of the URL consists of the following three parts:

1. Agreement (or service method)
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 directory and file names, etc.

URI parsing

Copy code The code is as follows:

"Resolving" a URI means converting a relative URI reference to its absolute form, or dereferencing the URI by attempting to obtain a dereferenceable URI or the resource represented by a URI reference. The "parsing" portion of document processing software often provides both capabilities.

Javascript URI parsing

Simply take the search JS in the blog as an example, the following is its URL,

http://www.jb51.net/search/?q=js&type=
Then there was

Copy code The code is as follows:

var parser = document.createElement('a');
parser.href = "http://www.jb51.net/search/?q=js&type="

We can then know its protocol, port number, host, specific address, etc.
Copy code The code is as follows:

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

The returned result is
Copy code The code is as follows:

protocol:http
host:www.jb51.net
pathname:/search/
search:?q=js&type=

The above results are added together to form a complete URI. I just don’t understand this part of parser.search very well. For the ? number, it should be a parameter, a parameter used for search.

If it is a URI for an email, assuming the URI is

Copy code The code is as follows:

mailto:h@jb51.net?subject=hello

Then
Copy code The code is as follows:

var parser = document.createElement('a');
parser.href = "mailto:h@jb51.net?subject=hello";

> parser.protocol
"mailto:"
> parser.pathname
"h@jb51.net"
> parser.search
"?subject=hello"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn