The problem of Chinese garbled characters in <a href>

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master semi-original article, without BO Master permission to reprint when allowed to reprint:)

When the href is required to pass the parameter, when the argument is Chinese, garbled problem will occur, the simplest way is:

The passed-in value is first encrypted with escape () and then decrypted with unescape () on the value page, and the pro-test is valid.

Check it out on the Internet. Also useful UrlEncode for encryption,UrlDecode decryption.

The amount ... So what's the difference between the two, a handy look at the collation, source (http://www.cnblogs.com/glory-jzx/archive/2013/06/14/3135580.html):

js to encode the text involves 3 functions : Escape,encodeuri,encodeuricomponent, the corresponding 3 decoding function: unescape,decodeuri,decodeuricomponent

1, passing parameters need to use encodeuricomponent, so that the combined URL will not be # and other special characters truncated.
For example: <script language= "JavaScript" >document.write (' <a href= ' http://passport.baidu.com/?logout&aid=7 &u= +encodeuricomponent ("http://cang.baidu.com/bruce42") + "> Exit </a> ');</script>


2, the URL to jump when you can use the whole encodeURI
For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");

3, JS Use the data can use escape
For example: Search the history record of the Tibetan.

4, Escape to the Unicode value other than 0-255 to encode the output%u**** format, in other cases escape,encodeuri,encodeuricomponent encoding results are the same.

5, the most used should be encodeuricomponent, it is the Chinese, Korean and other special characters into the utf-8 format of the URL encoding, So if you need to use encodeURIComponent to pass parameters to the background, you need the background decoding to Utf-8 support (the encoding in form is the same as the current page encoding method)

6. Do not encode:

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

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

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

"Escape () encodeURI () difference?"

Escape () Method:
All spaces, punctuation marks, accented characters, and any other non-ASCII characters are encoded in the form "%XX", and XX is represented by the 16 binary number.
Escape and Unescape encoding and decoding feature, Escape returns a 16 encoding of an ISO Latin character set . The Unescape function converts a 16-encoding with a special value into an ASCII string
Example:
Escape (' [email protected]#$%^&* () {}[]=:/;? +\ ' "'):
Results:%[email protected]%23%24%25%5e%26*%28%29%7b%7d%5b%5d%3d%3a/%3b%3f+%27%22

encodeURI () method
The encodeURI method returns an encoded URI. Therefore, if you use the decodeURI method for the result, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", "?". However, these characters can be encode using the encodeURIComponent method.
Encodes, a Uniform Resource Identifier (URI) replaces some characters one at a to describe the characteristics of UTF-8 encoding.
For example:
encodeURI (' [email protected]#$%^&* () {}[]=:/;?] +\ ' "'):
Result: [email protected]#$%25%5e&* ()%7b%7d%5b%5d=:/;? + '%22

encodeURIComponent () Method:
The encodeURIComponent method returns an encoded URI. So, if you will decodeURIComponent, the original string will return. Since all text encodesencodeuricomponent methods will be encoded, be careful if there are paths such as "/folder1/folder2/default." HTML ". After encryption does not occur, if the request as a network server will be invalidated. Use this method encodeURI when the string consists of more than one URI.
Example: The simplest way is to look at the code that they generate after encrypting these characters.
encodeURIComponent (' [email protected]#$%^&* () {}[]=:/;?] +\ ' "'):
Results!%40%23%24%25%5e%26* ()%7b%7d%5b%5d%3d%3a%2f%3b%3f%2b '%22

"When is the right way?"

The Escape () method does not encrypt + spaces on the server side that will be resolved to a space and in the forms in the form area. Based on this reduction, you should avoid using this method as much as possible, and the best way to use it is to choose one. encodeURIComponent ().

Escape () is not encrypted: @*/+

Using encodeURI () is slightly more specialized than escape (), which is coded for URIs. One as the opposite of QueryString, Belongs to a part of the URL. Using this method is when you need to convert a string to a URIs resource identity and require certain characters to remain in a non-encode state. Remember, this ' character is not encoded, because it is included in the URIs.

encodeURI () will not encrypt: [email protected]#$&* () =:/;? +

Finally, the encodeURIComponent () method is used in most cases, and when you need to encode a separate URIs part, this method encrypts some special URIs characters, so most parts can be contained inside. Remember, ' the character itself is included in the URIs, so this method does not encode it.

encodeURIComponent () does not encrypt:!* () '
http://shijian0306.javaeye.com/blog/241264

Bo Master only used escape, soplay a reference role

Get off work.

The problem of Chinese garbled characters in <a href>

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.