JavaScript encodeURIComponent ()

Source: Internet
Author: User

Encodes the string as a URI component.
There are three functions in JavaScript that can encode strings, namely: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri, decodeURIComponent.

Escape () escapes all incoming strings in addition to ASCII letters, numbers, and specific symbols, so it is best not to use this method if you want to encode the URL.

encodeURI () encodes the entire URI, because the legitimate characters in the URI are not converted by encoding.
,/?:@&=+$#. Rightentire URLencoding, and the specific identifier of the URL is not transcoded.

The encodeURIComponent method, which encodes a single uricomponent (the request parameter), should be the most common, escaping the Chinese and special characters in the argument without affecting the entire URL.
<a href= "http://passport.baidu.com/?logout&aid=7&u= ' +encodeuricomponent (" http://cang.baidu.com/ Bruce42 ") + ' > Exit </a> ');</script>
The 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.
The difference between the encodeURIComponent () function and the encodeURI () function.
encodeURIComponent () assumes that its arguments are part of a URI (such as a protocol, host name, path, or query string). Therefore, the encodeURIComponent () function escapes punctuation marks that are used to separate the various portions of the URI.

Return value: A copy of uristring, where some of the characters will be replaced by a hexadecimal escape sequence.
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().
Other characters (such as:;/?:@&=+$,# these are used toseparating punctuation from URI components), are made up of one or more hexadecimalEscape sequence SubstitutionOf

document.write (encodeURIComponent ("http://www.w3school.com.cn")) http%3a%2f%2fwww.w3school.com.cn
document.write (encodeURIComponent ("http://www.w3school.com.cn/P 1/")) http%3a%2f%2fwww.w3school.com.cn%2fp%201%2f
document.write (encodeURIComponent (",/?:@&=+$#"))%2c%2f%3f%3a%40%26%3d%2b%24%23

JavaScript encodeURIComponent ()

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.