Use of JavaScript encoding/Decoding Methods

Source: Internet
Author: User

JavaScript code involves three functions: escape, encodeURI, and encodeURIComponent. The corresponding three decoding functions are: unescape, decodeURI, and decodeURIComponent.

1. encodeURIComponent is required when passing parameters so that the combined url will not be truncated by special characters such.

Example: <script language = "javascript"> document. write ('<a href = "http://passport.baidu.com /? Logout & aid = 7 & u = '+ encodeURIComponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </script>

2. encodeURI can be used for url redirection.

Example: Location. href = encodeURI ("http://cang.baidu.com/do/s? Word = Baidu & ct = 21 ");

3. You can use escape [Huoho. Com] to edit js data.

For example, search for the history record in the bucket.

4. When encoding unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.

The most commonly used format is encodeURIComponent, which converts special characters such as Chinese and Korean into UTF-8 url encoding, therefore, if you want to use encodeURIComponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)

Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z

EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z

EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~, 0-9, a-z, A-Z

========================================================== ======================

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.

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.

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

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.