Analysis and comparison of escape (), encodeURI () and encodeURIComponent () in JavaScript

Source: Internet
Author: User

Escape (),  encodeuri (), and encodeURIComponent () are the three common methods used in JavaScript to encode strings, but their similarities and differences have plagued many JavaScript beginners, I'm here today to analyze and compare these three methods in detail.  escape ()   method  msdn jscript reference said: the escape method  returns a string value  (In unicode format)  that contains the  contents of [the argument]. All spaces, punctuation, accented  characters, and any other non-ascii characters are replaced with  %xx encoding, where xx is equivalent to the hexadecimal number  representing the character. for example, a space is returned  as  "%20."   The Escape method returns a string that contains the contents of the passed-in parameter in Unicode format. The  escape method replaces all whitespace, punctuation, accented characters, and any other non-ASCII characters in the passed-in parameter with the encoded form of%XX, where xx is the same as the 16-digit representation of the character it represents. If the 16 binary representation of the space character is 0x20, then xx should be 20, that is, Escape (" ?)   Return to "%20".  mozilLa developer core javascript guide said: the escape and unescape  Functions let you encode and decode strings. the escape function  returns the hexadecimal encoding of an argument in the iso  latin character set. the unescape function returns the ascii  string for the specified hexadecimal encoding value.  Escape and Unescape methods can help you encode and decode strings. The Escape method returns its 16 encoding for a parameter that consists of characters in the Iso latin character set. Correspondingly, the Unescape method can convert the parameters of the 16-encoding form into its ASCII form.  encodeuri () method  msdn jscript reference said: the encodeuri method  Returns an encoded uri. if you pass the result to decodeuri,  the original string is returned. The encodeURI method does  Not encode the following characters:  ":",  "/",  ";", and  "?".  Use encodeURIComponent to encode these characters.  The encodeURI method returns an encoded URI. If the encoding result of the encodeURI method is passed to the decodeURI method as a parameter, the original non-encoded string can be obtained. It is important to note that the encodeURI method does not encode the following characters ":",  "/",  ";", and  "?". If you want to encode these characters, use the encodeURIComponent method.  mozilla developer core javascript guide said: encodes a uniform  resource identifier  (URI)  by replacing each instance of certain  characters by one, two, or three escape sequences representing  the utf-8 encoding of the character.    

Escape (), encodeURI () and encodeURIComponent () in JavaScript

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.