JavaScript URL Chinese Character coding conversion

Source: Internet
Author: User

When using a URL for parameter passing, some Chinese name arguments or URL addresses are often passed, and conversion errors occur during background processing. In some pass-through pages use GB2312, and the Receive page uses UTF8, so that the received parameters may be inconsistent with the original. URLs that are encoded using the server-side UrlEncode function are not the same as the URLs encoded with the encodeURI function of client JavaScript. Huimin County House Zhe Clothing

JavaScript encodes text in 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent.

Effect Demo





Escape () method

Encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+

English explanation: MSDN JScript reference:the Escape method returns a string value (in Unicode format) that contains the contents of [t He argument]. All spaces, punctuation, accented characters, and any other non-ascii characters is replaced with%XX encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20." Edge Core Javascript guide:the escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of a argument in the ISO Latin character set. The Unescape function returns the ASCII string for the specified hexadecimal encoding value.

Is that JavaScript can use the Escape () function when using data.

Escape outputs the%u**** format when encoding Unicode values other than 0-255, and in other cases the Escape,encodeuri,encodeuricomponent encoding results are the same.

encodeURI () method

Converts the URI string into an escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? +

English explanation: MSDN JScript reference:the encodeURI method returns an encoded URI. If you pass the result of the decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript guide:encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters B Y one, one, or three escape sequences representing the UTF-8 encoding of the character.

encodeURI () can be used as a whole when URL jumps, such as:

1 Location.href=encodeURI("http://www.nowamagic.net/");
encodeURIComponent () method

Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )

English explanation: MSDN JScript reference:the encodeURIComponent method returns an encoded URI. If you pass the result of the decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as/folder1/ Folder2/default.html. The slash characters would be encoded and would is not being valid if sent as a request to a Web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript guide:encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, both, or three escape sequences representing the UTF-8 encoding of the character.

You need to use encodeURIComponent when passing parameters so that the combined URLs are not truncated by special characters such as #. For example:

1 <script language="javascript">
2 document.write(‘<a href="http://passport.nowamagic.net/?logout&aid=7&u=‘+encodeURIComponent("http://www.nowamagic.net/bruce42")+‘">退出</a>‘);
3 </script>

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

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

English Note: The Escape () method does not encode the + character which are interpreted as a space on the server side as well as Gen Erated by forms with spaces in their fields. Due to the shortcoming, you should avoid use of escape () whenever possible. The best alternative is usually encodeuricomponent (). Use of the encodeURI () method was a bit more specialized than escape () in that it encodes for URIs [REF] as opposed to the QueryString, which is part of a URL. Use this method when you need to encode a string to is used for any resource that uses URIs and needs certain characters T O remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within uris.lastly, the Encodeuricompone NT () method should is used in the most cases when encoding a single component of a URI. This method would encode certain chars that would normally being recognized as special chars for URIs so, many components May is included. Note that this method does not encode the ' character, as it is a valid character within URIs.

The most used should be encodeuricomponent, which is to convert 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 to have background decoding for UTF-8 support (the encoding in form is the same as the current page encoding method).

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 (str) method, which is used to escape any character that cannot be sent correctly in clear text. For example, a space in a phone number will be converted to character% 20, allowing these characters to be passed in the URL

If you need to send security information or XML, you might want to consider sending content using Send () (Security data and XML messages will be discussed in subsequent articles in this series). If you do not need to pass data through send (), simply pass NULL as a parameter to the method.

JavaScript URL Chinese Character coding conversion

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.