Escape (), encodeURI (), encodeURIComponent () The difference between the explanation (turn)

Source: Internet
Author: User

There are three functions in JavaScript that can encode strings, namely: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions:, decodeuri,decodeuricomponent.

Here's a brief look at the difference between them

1 Escape () function

Definition and usage

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

Grammar

Escape (String)

Parameter description

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

return value

A copy of the encoded string. Some of these characters are replaced with 16-binary escape sequences.

Description

The method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation marks:-_. ! ~ * ' (). All other characters will be replaced by escape sequences.

2 encodeURI () function

Definition and usage

The encodeURI () function encodes a string as a URI.

Grammar

encodeURI (uristring)

Parameter description

uristring required. A string that contains the URI or other text to encode.

return value

A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Description

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

The purpose of this method is to fully encode the URI, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,#

3 encodeURIComponent () function

Definition and usage

The encodeURIComponent () function encodes a string as a URI component.

Grammar

encodeURIComponent (uristring)

Parameter description

uristring required. A string that contains the URI component or other text to encode.

return value

A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Description

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

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

Hints and Notes

Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.

4 Summary:

Through the analysis of three functions, we can know: Escape () In addition to ASCII letters, numbers and specific symbols, the passed-in strings are all escaped encoding, so 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 legitimate characters in the URI are not converted by encoding. The encodeURIComponent method encodes a single uricomponent (the request parameter) should be the most common, it can be described in the parameters of Chinese, special characters escaped, without affecting the entire URL.

Escape outputs the%u**** format when encoding Unicode values other than 0-255, and in other cases the Escape,encodeuri,encodeuricomponent encoding results are the same.

Escape does not encode characters with 69: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

Although escape (), encodeURI (), and encodeURIComponent () Three methods can be used to affect the URL integrity of a number of special

characters for filtering. But the latter two is to convert the string to UTF-8 way to transfer, resolved the page encoding mixed to cause the garbled question

Problem. For example, the Send page is inconsistent with the encoding format (Charset) of the Accept page (assuming that the sending page is GB2312 and the Receive page encoding is

UTF-8), using Escape () to convert the text string in the transmission will cause garbled problems.

Here are the various ways to encode/decode URLs under JS:

Escape Method: Returns an encoded String object that can be read on all computers.

function Escape (charstring:string): String

Characters that are not encoded by this method: @ */+

Description: The Escape method returns a string value (in Unicode format) that contains charstring content. All spaces, punctuation,

Accented symbols and any other non-ASCII characters are replaced with%XX encoding, where xx equals the hexadecimal number that represents the character.

For example, a space is returned as "". (characters with a character value greater than 255 are stored in%uxxxx format.) )

Note: The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encoding should use the encodeURI and

encodeURIComponent method

encodeURI Method: Returns a string encoded as a valid Uniform Resource Identifier (URI).

function encodeURI (uristring:string): String

Characters that will not be encoded by this method:! @ # $ & * () =:/;? +

Description: The encodeURI method returns an encoded URI. If the encoding result is passed to decodeURI, the initial

String. encodeURI does not encode the following characters: ":", "/", ";" and "?". Please use

These characters are encoded by the encodeuricomponent.

encodeURIComponent Method: Returns a string of valid components encoded as a Uniform Resource Identifier (URI).

function encodeURIComponent (encodeduristring:string): String

Characters that will not be encoded by this method:! * ( ) ‘

Description: The encodeURIComponent method returns an encoded URI. If you pass the encoding result to the

decodeURIComponent, the initial string is returned. Because the encodeURIComponent method encodes all characters,

Note that if the string represents a path, such as/folder1/folder2/default.html, the slash will also be

encoding, so that it will be invalid when the string is sent as a request to the WEB server. If the string contains more than one URI

component, use the encodeURI method to encode.

Method: Returns the decoded string from a string object encoded with the escape method.

function (charstring:string): String

Description: The method returns a string value that contains the contents of the charstring. All encoded in%XX 16 binary form

Characters are replaced by the equivalent characters in the ASCII character set. (characters encoded in%uxxxx format (Unicode characters) with 16

The Unicode character of the binary code xxxx is replaced. )

Note: The method should not be used to decode the Uniform Resource Identifier (URI). Instead, use decodeURI and

decodeURIComponent method.

decodeURI method: Returns the non-encoded form of an encoded Uniform Resource Identifier (URI).

function decodeURI (uristring:string): String

decodeURIComponent method: Returns the non-encoded form of an encoded component of a Uniform Resource Identifier (URI).

function decodeURIComponent (encodeduristring:string): String

Escape (), encodeURI (), encodeURIComponent () The difference between the explanation (turn)

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.