Javascript URI parsing Introduction _javascript Tips

Source: Internet
Author: User

Parsing a URI is an interesting thing, and before you know it, it can be so complicated.

Uri

The explanation for URIs in Wikipedia is this:

Copy Code code as follows:

In computer terminology, a Uniform Resource identifier (uniform Resource Identifier, or URI) is a string that identifies the name of an Internet resource. This identification allows the user to interact with the resources of the network (generally the World Wide Web) through specific protocols. The URI is defined by a scenario that includes a determination of the syntax and related protocols.

A quote from the network's interpretation of the composition of URIs, which can be seen later in the parsing of URIs.

A URI generally consists of three parts:

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

Or it could be said that the two seem to be the same.

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

1. Agreement (or service method)
2. Host IP address (sometimes including port number) for which the resource exists
3. The specific address of the host resource. , such as directory and file name, etc.

URI parsing

Copy Code code as follows:

Parsing a URI means converting a relative URI reference to an absolute form, or by trying to obtain a resource represented by a solvable URI or a URI reference, to dereference the URI. The "parsing" section of the document processing software usually provides both functions.

Javascript URI Parsing

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

Http://www.jb51.net/search/?q=js&type=
And then there's the

Copy Code code as follows:

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

We can know its agreement, port number, host, specific address, etc.
Copy Code code as follows:

Parser.protocol;
Parser.host;
Parser.pathname;
Parser.search;

The result of the return is
Copy Code code as follows:

Protocol:http
Host:www.jb51.net
pathname:/search/
Search:?q=js&type=

The above result is added, is a complete URI. Just for Parser.search this part is not very understanding, for the? number, should be parameters, for the search parameters.

If it's a URI for a message, suppose the URI is

Copy Code code as follows:

Mailto:h@jb51.net?subject=hello

So
Copy Code code 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"

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.