JavaScript encoding and decoding

Source: Internet
Author: User
1. The escape () function can encode the string so that the string can be read on all computers. This method does not encode ASCII letters and numbers or the following ASCII punctuation symbols + /-*_.@. All other characters will be... syntaxHighlighter. all (); 1. the escape () function can encode a string so that the string can be read on all computers. This method does not encode ASCII letters and numbers or the following ASCII punctuation symbols + /-*_.@. All other characters are replaced by escape sequences. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. When encoding unicode values other than 0-, the output format is % u ***. For example, the encoded Value of "I" is % u6211. 2. The encodeURI () function can encode a string as a URI. Some Characters in the string are replaced by the hexadecimal escape sequence. 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 :;/?: @ & =+ $, #3. encodeURIComponent () method: converts a URI string into an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks -*_.!~ '(). Conclusion: 1. escape () Outputs % u *** format when encoding unicode values other than 0-. In other cases, escape, encodeURI, and encodeURIComponent encode special characters with the same result, the same result indicates that the characters are within the encoding range of the three functions at the same time, such as spaces. It does not make sense to process not three functions at the same time. 2. escape () is an old function, so try not to use it. When URI is encoded, because escape encodes common characters in URI, such /? So encodeURI () should be selected for URI encoding. When encoding the form field submitted by the user, we know that in the form field submitted by the user, if there is a space, it will be converted to a + character, and the server will think that the + number represents a space during parsing. However, escape () does not encode '+', so encodeURIComponent () should be selected (). 3. Because encodeURI () does not encode common characters in the URL :;/?: @ & =+ $, #, And escape () and encodeURIComponent () encode these special characters. Therefore, encodeURI () should be selected when URI is encoded (). 4. The encodeURIComponent method is the most commonly used method for encoding a single URIComponent (request parameter. Because if the parameter contains? & And other special characters, encodeURI will not encode them. This may cause an error in parsing Request Parameters. For the reason that escape () is not used, see the first in the summary. 5. the decoding functions corresponding to the three functions are unescape (), decodeURI (), and decodeURIComponent (). 6. in many cases, the browser will automatically encode the data, such as the URL. Therefore, it is best to encode the data before transmitting the data, and decode the data with the corresponding decoding function after receiving the data, this effectively ensures data consistency.

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.