Several methods of javascript encoding are described in detail.

Source: Internet
Author: User

Escape definition and usage

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

Parameters Description
String Required. The string to be escaped or encoded.

Return Value

EncodedString. Some characters are replaced with hexadecimal escape sequences.

Description

This method does not encode ASCII letters and numbers or the following ASCII punctuation characters: * @-_ + ./. All other characters are replaced by escape sequences.

Tips and comments

Tip: You can use unescape () to decode the escape () encoded string.

Note: ECMAScript v3 rejects this method. The application uses decodeURI () and decodeURIComponent () to replace it.

Example:

 

EncodeURI definition and usage

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

Syntax encodeURI ( URIstring)
Parameters Description
URIstring 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 :;/? : @ & =+ $ ,#

Tips and comments

You can use decodeURI () for decoding.

Tip: If the URI component contains a separator, such? And #, use the encodeURIComponent () method to encode each component.

Example

Definition and usage

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

Syntax
encodeURIComponent(URIstring)
Parameters Description
URIstring 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

Prompt: 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.

Example

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.