Js coding and decoding

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, in this way, 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 & gt ;'); </script> 2. encodeURI can be used as a whole during url jump, for example, Location. href = encodeURI (http://cang.baidu.com/do/s? Word = China & ct = 21); the characters encoded using this method can use urldecode () in PHP () function anti-encoding 3. When JavaScript uses data, you can use escape to encode unicode values other than 0-and output the % u *** format. In other cases, escape, encodeURI, the encoding result of encodeURIComponent is the same. Escape has 69 unencoded characters: *, + ,-,., /, @, _, 0-9, a-z, A-Z encodeURI unencoded characters include 82 :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,?, @,_,~, 0-9, a-z, A-Z encodeURIComponent unencoded characters 71 :!, ',(,),*,-,.,_,~, 0-9, a-z, An Introduction to the three functions attached to the A-Z: the escape method encodes String objects so that they can be read on all computers, escape (charString) the required charstring parameter is any String object or text to be encoded. Description: The escape method returns a string containing the charstring content (in Unicode format ). All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % xx encoding. xx equals to the hexadecimal number of the character. For example, "% 20" is returned by a space ". The characters with a 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. The encodeURI method encodes a text string into a valid Uniform Resource Identifier (URI ). The URIString parameter required by encodeURI (URIString) represents an encoded URI. The encodeURI method returns an encoded URI. If you pass the encoding result to decodeURI, the initial string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "? ". Use the encodeURIComponent method to encode these characters. The encodeURIComponent method encodes text strings into a valid component of a unified Resource Identifier (URI. EncodeURIComponent (encodedURIString) the required encodedURIString parameter represents an encoded URI component. The encodeURIComponent method returns an encoded URI. If you pass the encoding result to decodeURIComponent, the initial string is returned. Because 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. In this way, when the encoding result is sent to the web server as a request, it will be invalid. If the string contains more than one URI component, use the encodeURI Method for encoding.

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.