Escape (), encodeURI (), encodeURIComponent () encoding and decoding

Source: Internet
Author: User

1 Escape (), encodeURI (), encodeuricomponent () Differences2 3 There are three functions in JavaScript that can encode strings, namely: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri, decodeURIComponent. 4 5 Here's a brief look at the difference between them6 71Escape () function8 9 Definition and UsageTen The escape () function encodes the string so that it can be read on all computers.  One  A Grammar - Escape (String) -  the parameter Description - string is required. The string to be escaped or encoded.  -  - return value + a copy of the encoded string. Some of these characters are replaced with 16-binary escape sequences.  -  + Description AThe method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation marks:-_. ! ~ * '(). All other characters will be replaced by escape sequences.  at  -   -  -  - 2 encodeURI () function - Definition and Usage in the encodeURI () function encodes a string as a URI.  -  to Grammar + encodeURI (uristring) -  the parameter Description * uristring required. A string that contains the URI or other text to encode.  $ Panax Notoginseng return value - A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.  the  + Description AThe method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * '().  the  +The purpose of this method is to fully encode the URI, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the URI:;/?:@&=+$,# -  $   $  -  -3encodeuricomponent () function the  - Definition and UsageWuyi the encodeURIComponent () function encodes a string as a URI component.  the  - Grammar Wu encodeURIComponent (uristring) -  About parameter Description $ uristring required. A string that contains the URI component or other text to encode.  -  - return value - A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.  A  + Description theThe method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * '().  -  $ other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI component) are replaced by one or more hexadecimal escape sequences.  the  the Hints and notes the Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.  the  -   in  the 4 Summary: the  About through the analysis of three functions, we can know: Escape () In addition to ASCII letters, numbers and specific symbols, the passed-in strings are all escaped encoding, so if you want to encode the URL, it is best not to use this method. encodeURI () is used to encode the entire URI because the legitimate characters in the URI are not converted by encoding. The encodeURIComponent method encodes a single uricomponent (the request parameter) should be the most common, it can be described in the parameters of Chinese, special characters escaped, without affecting the entire URL.  the  the   the  + 5 Example: -  the 1 Escape ()Bayi  the <script type= "Text/javascript" > the  - document.write (Escape ("http://www.w3school.com.cn/") + "<br/>") -  the document.write (Escape ("?! = () #%& ")) the  the </script> Output: the  - http%3a//www.w3school.com.cn the  the %3f%21%3d%28%29%23%25%26 the 94 2 encodeURI () the  the <script type= "Text/javascript" > the 98 document.write (encodeURI ("http://www.w3school.com.cn/") + "<br/>") About  - document.write (encodeURI ("Http://www.w3school.com.cn/My first/"))101 102 document.write (encodeURI (",/?:@&=+$#"))103 104 </script> Output: the 106 http://www.w3school.com.cn/107 108 http://www.w3school.com.cn/My%20first/109  the ,/?:@&=+$#111  the The entire URL is encoded, and the specific identifier for the URL is not transcoded. 113  the 3 encodeURIComponent () the  the Example 1:117 118 <script type= "Text/javascript" >119  - document.write (encodeURIComponent ("http://www.w3school.com.cn/"))121 122 document.write ("<br/>")123 124 document.write (encodeURIComponent ("http://www.w3school.com.cn/p 1/")) the 126 document.write ("<br/>")127  - document.write (encodeURIComponent (",/?:@&=+$#"))129  the </script Output:131  the http%3a%2f%2fwww.w3school.com.cn133 http%3a%2f%2fwww.w3school.com.cn%2fp%201%2f134 %2c%2f%3f%3a%40%26%3d%2b%24%23135Example 2:<script language= "JavaScript" >document.write ('136 137<a href= "http://passport.baidu.com/?logout&aid=7&u= ' +encodeuricomponent (" http://cang.baidu.com/bruce42 ") + ' > Exit </a> ');</script>138 139The parameters in the URL are encoded because the parameter is also a URL, and if not encoded, it affects the entire URL of the jump.

Escape (), encodeURI (), encodeURIComponent () encoding and decoding

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.