In-depth analysis of the differences between escape (), encodeURI (), and encodeURIComponent () and example _ javascript skills

Source: Internet
Author: User
This article mainly introduces the differences between escape (), encodeURI (), and encodeURIComponent (). For more information, see the following three functions in JavaScript that can encode strings: escape, encodeURI, and encodeURIComponent. Three decoding functions are involved: unescape, decodeURI, and decodeURIComponent.

The following is a brief introduction of their differences:

1Escape () function

Definition and usage

The escape () function can encode a string so that the string can be read on all computers.

Syntax

Escape (string)

Parameter description

String is required. The string to be escaped or encoded.

Return Value

A copy of the encoded string. Some characters are replaced with hexadecimal escape sequences.

Description

This method does not encode ASCII letters and numbers or the following ASCII punctuation marks :-_.! ~ *'(). All other characters are replaced by escape sequences.

2EncodeURI () function

Definition and usage

The encodeURI () function can encode a string as a URI.

Syntax

EncodeURI (URIstring)

Parameter description

URIstring is required. A string containing URI or other text to be encoded.

Return Value

A copy of URIstring, some of which will be replaced by the hexadecimal escape sequence.

Description

This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

The purpose of this method is to fully encode the URI. Therefore, the encodeURI () function will not escape the following ASCII punctuation marks with special meanings in the URI :;/? : @ & =+ $ ,#

3EncodeURIComponent () function

Definition and usage

The encodeURIComponent () function can encode a string as a URI component.

Syntax

EncodeURIComponent (URIstring)

Parameter description

URIstring is required. A string containing the URI component or other text to be encoded.

Return Value

A copy of URIstring, some of which will be replaced by the hexadecimal escape sequence.

Description

This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().

Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences.

Tips and comments

Tip: note the differences between the encodeURIComponent () function and the encodeURI () function. The former assumes that its parameters are part of the URI (such as the protocol, host name, path, or query string ). Therefore, the encodeURIComponent () function uses escape characters to separate the punctuation marks of each part of the URI.

4Summary:

By analyzing the three functions, we can know that in addition to ASCII letters, numbers, and specific symbols, escape () can escape and encode all the passed strings, therefore, if you want to encode the URL, it is best not to use this method. EncodeURI () is used to encode the entire URI, because the valid characters in the URI are not encoded and converted. The encodeURIComponent method is the most commonly used method for encoding a single URIComponent (request parameter). It can escape Chinese and special characters in parameters without affecting the whole URL.

5Example:

1 escape ()

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.