Java coding in JS

Source: Internet
Author: User
Tags decode all

Java coding in JS

In the current web applications, more and more page elements are operated by js, especially when an asynchronous request is initiated by js, encoding problems often occur. The following describes the Encoding Problems in js.

1. Introduce js files externally

Contains string input in a separate js file, for example:

 

If a script. js script is introduced, the script contains the following code:

 

Document. write (this is a Chinese text); // document. getElementById ('testid'). innerHTML = 'This is a Chinese character ';

 

If charset is not set for the script, the browser will parse the js file using the default Character Set of the current and current pages. If the external js file encoding format is consistent with the current page encoding format, you can leave this charset unspecified. However, if the script. js file and the current page encoding format is inconsistent, such as script. js is UTF-8 encoding and the page is GBK encoding, the Chinese input in the above Code will become garbled.

 

2. js url Encoding

The default encoding of the URLs that initiate asynchronous calls through js is also affected by the browser, for example, the original ajax http_request.open ('get', url, true) Call, the URL encoding is the default Operating System encoding in IE, while the UTF-8 encoding in FireFox. In addition, different JS frameworks may also process URl encoding. So, how to deal with the js url encoding problem?

In fact, there are three functions for processing URL encoding in JS. As long as you master these three functions, you can basically correctly handle js url garbled issues.

 

1. escape ()

This function uses ASCII letters, numbers, and punctuation marks (* + -. /@ _) and others are converted to Unicode encoding values at your own discretion, and "% u" is added before the encoding value, as shown in.

 

By converting special characters into Unicode encoding values, you can avoid information loss due to incompatible character sets. You can avoid garbled characters by decoding parameters on the server.

Note that escape () and unecape () have been removed from the ECMAScript v3 standard. The URL encoding can be replaced by encodeURI () and encodeURIComponent.

 

2. encodeURI ()

Compared with escape (), encodeURI () is a real JS function used for URL encoding. It can include characters in the whole URL (except for some special characters, such as "!" # $ & '() * +,-./:; =? @_~ 0-9 a-z A-Z) for UTF-8 encoding, add "%" before each code value, as shown in.

 

Decode through the decodeURI function, as shown in.

 

3. encodeURICompoent ()

The encodeURIComponent () function is more thorough than encodeURI () encoding, except for! ,()*-._~ 0-9 a-z A-Z these characters are not encoded, all other characters are encoded. This function is usually used to place a URL as a parameter in another URL, as shown in.

 

It can set HTTP: // localhost/servlet/junshan? A = c & a = B is placed in another URL as a parameter. If encodeURIComponent () is not encoded, "&" in the following URL will affect the integrity of the previous URL. DecodeURIComponent () is used for encoding, as shown in.

 

4. Repeated Decoding of java and js

The three functions mentioned above are all processed by JS. If JS is encoded and the encoded characters can be decoded by Java after being uploaded to the server, how can Java be decoded?

We know that there are two classes for processing URL Codec in Java: java.net. URLEncoder and java.net. URLDecoder. These two classes can decode all % plus UTF-8 values with UTF-8 to get the original character. View the source code of URLEncoder. You can find that the special characters protected by URLEncoder are less than those protected by JS. The URLEncoder and URLDecoder of the Java end correspond to encodeURIComponent and decoderURIComponent of the front-end JS. Note: After the front end is encoded with encodeURIComponent, garbled characters may occur when URLDecoder is used for decoding on the server end, which must be caused by inconsistent encoding of the two characters. JS encoding is the default UTF-8 encoding, and the server Chinese decoding is generally GBK or GB2312, so after using encodeURIComponent encoding is UTF-8, and Java to decode using GBK is obviously wrong. The solution is to use encodeURIComponent for two encoding times, such as encodeURIComponent (str )). In this way, the Java end uses request. getParamter () is a UTF-8 encoded string obtained after Decoding with GBK, if the Java end needs to use this string, then use the UTF-8 to decode once; if it is to output this result directly through JS to the front end, then this UTF-8 string can be directly displayed on the front end normally.

 

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.