UrlEncode and encodeURIComponent function usage in JavaScript

Source: Internet
Author: User
Tags urlencode

JS encoding text involves 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent

You need to use encodeURIComponent when passing parameters so that the combined URL will not be truncated by special characters such as #.

For example: <script language= "JavaScript" >document.write (' <a href= '/?logout&aid=7& u= ' +encodeuricomponent ("www.111cn.net") + ' "> Exit </a& gt; '); </script>

encodeURI can be used as a whole when URL jumps are made

For example: location.href= "/encodeuri" ("/s?word= Baidu &ct=21");

JS can use escape when using data

For example: History records in the search of Tibet.

Escape encodes a Unicode value other than 0-255 by outputting the%u**** format, in other cases the Escape,encodeuri,encodeuricomponent encoding results are the same.

The most used should be encodeuricomponent, which is to convert Chinese, Korean and other special characters into utf-8 format URL encoding, So if you need to use encodeURIComponent when passing parameters to the background, you need background decoding for utf-8 support (the form is encoded in the same way as the current page encoding)

Escape does not encode 69 characters: *+-./@_0-9a-za-z

encodeURI characters are not encoded with 82:!#$& ' () *+,-./:;=?@_~0-9a-za-z

There are 71 encodeuricomponent characters that are not encoded:! ' () *-._~0-9a-za-z

According to the instructions, I need the encodeURIComponent function.

According to the above mentioned

1. Browser, the URL of the form is the same as the page encoding

2. URLs sent in browsers using XMLHTTP are consistent with the browser default settings

3. The request URL and the server are consistent without garbled appearance

PS: The recommended use of encodeURIComponent (), with its encoded URL and PHP UrlEncode () function results consistent with the most convenient interaction.

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.