A summary of the encoding method of JS string

Source: Internet
Author: User

In encoding URL strings with JavaScript, although escape (), encodeURI (), and encodeURIComponent () Three methods can filter some special characters that affect URL integrity. But the latter two is the way to convert the string to UTF-8, which solves the problem of garbled page encoding. For example, the sending page is inconsistent with the encoding format (Charset) of the Accept page (assuming that the sending page is GB2312 and the Receive page encoding is utf-8), using Escape () to convert the text string in transit will cause garbled problems. The following is JS under the URL to be compiled/decoding the various methods:1, Escape method: Returns an encoded String object that can be read on all computers. function Escape (charstring:string): string is not encoded by this method: @* / +Description: The Escape method returns a string value (in Unicode format) that contains charstring content. All spaces, punctuation, accent marks, and any other non-ASCII characters are used%xx code substitution, where xx equals the hexadecimal number that represents the character. For example, a space is returned as "% -”。 (The character value is greater than255The characters in%uxxxx format storage. Note: The escape method cannot be used to encode a Uniform Resource Identifier (URI). The encoding should use the encodeURI and encodeURIComponent methods. 2, encodeURI Method: Returns a string encoded as a valid Uniform Resource Identifier (URI). function encodeURI (uristring:string): A string that is not encoded by this method:! @ # $ & * () =:/;? +'Description: 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 "?". Please use encodeURIComponent to encode these characters. 3, encodeURIComponent method: Returns a string of valid components encoded as a Uniform Resource Identifier (URI). function encodeURIComponent (encodeduristring:string): A string that is not encoded by this method:! * ( )' Description: The encodeURIComponent method returns an encoded URI. If the encoding result is passed to decodeURIComponent, the initial string is returned. Because the encodeURIComponent method encodes all characters, be aware that if the string represents a path, for example/folder1/folder2/default. html, the slash in it will also be encoded so that it will be invalid when the string is sent as a request to the WEB server. If the string contains more than one URI component, use the encodeURI method to encode it. 4, unescape method: Returns the decoded string from a string object encoded with the escape method. function unescape (charstring:string): String Description: The Unescape method returns a string value that contains the charstring content. All in%the characters encoded in the XX 16 binary form are replaced by the equivalent characters in the ASCII character set. (In%the characters encoded in the UXXXX format (Unicode characters) are replaced with hexadecimal-encoded XXXX Unicode characters. Note: The Unescape method should not be used to decode the Uniform Resource Identifier (URI). Use the decodeURI and decodeURIComponent methods instead. 5, decodeURI method: Returns the non-encoded form of an encoded Uniform Resource Identifier (URI). function decodeURI (uristring:string): String decodeURIComponent Method: Returns the non-encoded form of an encoded component of a Uniform Resource Identifier (URI). function decodeURIComponent (encodeduristring:string): A method for URL encoding in String btw:c# ... Code: Server.URLEncode (string) Decoding: Server.urldecode (stringThe previous three types of client-side encoding can be decoded in the background using this method.

A summary of the encoding method of JS string

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.