Differences between escape, encodeURI, encodeURIComponent, and other methods

Source: Internet
Author: User

For ASCII characters, these three functions are used to convert characters into Percent-encoding (Percent-encoding). The difference is that the characters are different in their respective exclusion codes:

Escape () will not encode: @ */+

EncodeURI () will not encode :~! @ # $ & * () = :/,;? +'

EncodeURIComponent () will not encode :~! *()'

In addition, escape is actually a window object method. encodeURIComponent and encodeURI are JS built-in functions.

Information in MDC: window. escape and Global_Functions/encodeURIComponent.

For non-ASCII characters, escape and encodeURIComponent are significantly different:

Escape ('carved ') =' % u96D5'

EncodeURIComponent ('carved ') =' % E9 % 9B % 95'

% U96D5 is not a standard Pecent-encoding, and no standard support is available now.

The simplest method for anti-transcoding is to use unescape:

Alert (unescape ('% u96D5 ');

Escape Method

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

Function escape (charString: String): String

Parameters

CharString

Required. Any String object or text to be encoded.

Remarks

The escape method returns a string containing charstring content (in Unicode format ). All spaces, punctuation marks, accents, and any other non-ASCII characters are replaced by % xx encoding, where xx equals to the hexadecimal number of the character. For example, a space is returned as "% 20 ".

Characters with a character value greater than 255 are stored in % uxxxx format.

Note that the escape method cannot be used to encode the Uniform Resource Identifier (URI. Use the encodeURI and encodeURIComponent methods for encoding.

Requirements

Version 1

See

EncodeURI method | encodeURIComponent method | String object | unescape Method

Applicable to: Global Objects

EncodeURI Method

Returns a string encoded as a valid Uniform Resource Identifier (URI.

Function encodeURI (URIString: String): String

Parameters

URIString

Required. Indicates the URI-encoded string.

Remarks

The encodeURI method returns an encoded URI. If the encoding result is passed to decodeURI, the initial string is returned. EncodeURI does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters.

Requirements

Release 5.5

See

DecodeURI method | decodeURIComponent Method

Applicable to: Global Objects

EncodeURIComponent Method

Returns the string of a valid component encoded as a uniform resource identifier (URI.

Function encodeURIComponent (encodedURIString: String): String

Parameters

EncodedURIString

Required. Indicates the string that encodes the URI component.

Remarks

The encodeURIComponent method returns an encoded URI. If the encoding result is passed to decodeURIComponent, the initial string is returned. The encodeURIComponent method encodes all characters. Note that if the string represents a path, for example,/folder1/folder2/default.html, the Slash will also be encoded, when this string is sent to the Web server as a request, it will be invalid. If the string contains multiple URI components, use the encodeURI Method for encoding.

Requirements

Release 5.5

See

DecodeURI method | decodeURIComponent Method

Applicable to: Global Objects

Unescape Method

Returns the decoded String from the String object encoded using the escape method.

Function unescape (charString: String): String

Parameters

CharString

Required. String object or text to be decoded.

Remarks

The unescape method returns a string value containing charstring content. All characters encoded in % xx hexadecimal format are replaced by equivalent characters in the ASCII character set.

Characters encoded in % uxxxx format (Unicode character) are replaced by Unicode characters encoded in hexadecimal format xxxx.

Note that the unescape method should not be used to decode the Uniform Resource Identifier (URI ). Use the decodeURI and decodeURIComponent methods.

Requirements

Version 1

See

DecodeURI method | decodeURIComponent method | escape method | String object

Applicable to: Global Objects

DecodeURI Method

Returns the unencoded format of an encoded Uniform Resource Identifier (URI.

Function decodeURI (URIstring: String): String

Parameters

URIstring

Required. Indicates the URI-encoded string.

Remarks

Use the decodeURI method to replace the obsolete unescape method.

The decodeURI method returns a string value.

If URIString is invalid, A URIError occurs.

Requirements

Release 5.5

See

DecodeURIComponent method | encodeURI Method

Applicable to: Global Objects

DecodeURIComponent Method

Returns the non-encoding form of an encoded component of the Uniform Resource Identifier (URI.

Function decodeURIComponent (encodedURIString: String): String

The required encodedURIString parameter is the value of an encoded URI component.

Remarks

URIComponent is part of a complete URI.

If encodedURIString is invalid, A URIError is generated.

Requirements

Release 5.5

See

DecodeURI method | encodeURI Method

Applicable to: Global Objects

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.