JS file encoding and conversion graphic tutorial

Source: Internet
Author: User

In Baidu and Google's search, we all say that we can solve the encoding problem through Java background processing. Can this solve the encoding problem of JS files?

The reason why the Javascript file encoding problem is raised is that the Javascript file encoding is different, leading to garbled Chinese characters.

Usually, create a JS file (containing Chinese characters) in eclipse. The text displayed in the eclipse editor is normal, but it is garbled on the webpage, and my pages are all encoded using UTF-8 encoding, so I think, this may be caused by the JS file encoding problem.


In eclipse, Chinese characters in JS files are displayed normally


Chinese characters are garbled on the webpage.

I think this may be caused by the encoding problem of the JS file itself. In fact, this is correct. Here we need to use a small tool named notepad2, which is translated as the first generation of notepad. This tool is easy to use and can be installed in green without installation. It can completely replace the built-in notepad software in windows. It's: http://download.csdn.net/source/1732849

Use notepad2 to open the JS file, view the JS file encoding, and find that the JS file encoding is ANSI, And the ANSI encoding is 0x80 ~ The two bytes in the 0xff range indicate 1 character. For example, the Chinese character "medium" is stored in the Chinese operating system using the two bytes [0xd6, 0xd0. However, different countries and regions have different ANSI standards, making different ANSI codes incompatible. As we all know, in China, the browser encoding is either GBK or gb2312 or utf8. When it is sent to a webpage in ANSI-Encoded chinese, it is naturally garbled, so this is the root of the problem.

With the help of notepad2 software, the JS file encoding into UTF-8 encoding, because UTF-8 encoding is a general computer code, can support almost all countries or regions of the language, and the browser itself supports UTF-8 encoding.

OK. Let's take a look at the effect on the browser. Obviously, Chinese characters are displayed normally and garbled characters are resolved. It is not as complicated and difficult to use the encodeuricomponent or encodeuri method in JS as it is mentioned on the Internet, but also complicated programming.

In my test, whether it is to use the new text document and then change the extension. JS, or JS files created using other software or tools, opened with notepad2 to view the encoding, and found that the JS file encoding is ANSI, so I guess, is the default encoding of JS files ANSI encoded? Of course, I did not go to the research. I have no relevant information on the Internet to prove that the default encoding of JS files is ANSI encoding. I only guess it.

However, at least, if the Chinese characters in the JS file are garbled on the webpage, you may wish to use thisArticleTo solve the headache.

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.