A detailed explanation of JavaScript URL encoding and anti-coding usage

Source: Internet
Author: User

These days with Ajax do not refresh the page, check the relevant data: JS encoding the text involves 3 functions:

Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent

Escape () Method:

The specified string is encoded using the ISO Latin character set. All spaces, punctuation, special characters, and other non-ASCII characters will be converted into%XX-formatted character encodings (XX equals the encoded 16 digits of the character in the character set table). For example, spaces's corresponding encoding is%20.

Characters that will not be encoded by this method: @ */+

Escape does not encode 69 characters: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI () Method:

Converts the URI string into a string in escape format using the UTF-8 encoding format.

Characters that will not be encoded by this method:! @ # $& * () =:/;? + '

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

encodeURIComponent () Method:

Converts the URI string into a string in escape format using the UTF-8 encoding format. This method encodes more characters than encodeURI (), such as/or characters. So if a string contains several parts of the URI, this method cannot be encoded, otherwise the URL will display an error after the/character is encoded.

Characters that will not be encoded by this method:! * ( ) '

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

Therefore, for Chinese strings, you only need to use escape if you do not want to convert the string encoding format into a UTF-8 format (such as when the original page and the target page charset are consistent). If your page is GB2312 or other code, and the page that accepts the parameter is UTF-8 encoded, you should use encodeURI or encodeuricomponent.

In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape was in the javascript1.0 version.

Cases

The code is as follows Copy Code

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<TITLE>JS Ajax encodeuricomponent</title>

<body>

var url = "Post.php?name=" +encodeuricomponent (names) + "&mo=" +encodeuricomponent (MOS) + "&body=" + encodeURIComponent (bodys) + "&id=" +<?= $id;? >;

Ajax part is not spoken, the main encodeuricomponent used to be garbled
</body>

PHP processing method,

  code is as follows copy code

$name = Iconv (' Utf-8 ', ' gb2312 ', Postget (' name '));

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.