The Chinese in the JS file is displayed as a garbled solution on the Web page

Source: Internet
Author: User

JS code conversion, this sentence itself is a sentence with two meanings. Usually understood as JS file can convert code, but, I encountered the problem is not so, is to solve the JS file itself coding problem, it is UTF-8 encoded or ANSI code? In Baidu, Google on a search, we all say that through the Java background processing to solve the coding problem, ask, This can solve the JS file itself coding problem?

The reason for the JS file itself coding problem, because JS file encoding is different, resulting in the Chinese display as garbled.

Usually, in eclipse to create a JS file (including Chinese), in the Eclipse editor to see the Chinese are very normal, but displayed on the Web page is garbled, and my page is all using UTF-8 code encoding, for this, I think, This may be the JS file itself caused by coding problems.

In Eclipse, the Chinese in the JS file is displayed correctly

In the Web page, the Chinese is displayed as garbled

I think this may be due to the JS file itself coding problems caused, in fact, this is correct. Here to use a small tool, called NotePad2, translation is the first generation of Notepad, this gadget is very useful, green installation-free, can replace the Windows system with Notepad software. Its download address is: http://download.csdn.net/source/1732849

With NotePad2 Open JS file, view the code of JS file, found that the encoding of JS files is ansi,ansi encoded 0x80~0xff range of 2 bytes to represent 1 characters. For example, Chinese character "medium" is stored in the Chinese operating system using the two bytes of [0xd6,0xd0]. However, different ANSI standards have been developed for various countries and regions, making different ANSI encodings incompatible. As we all know, in China, the browser's encoding is either GBK or GB2312 encoding, or UTF8 encoding, when the ANSI-encoded Chinese sent to the Web page, the natural display as garbled, so this is the root of the problem.

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

OK, and then look at the effect on the browser. Obviously, Chinese display normal, garbled problem solved. is not like the internet said "use JS in the encodeURIComponent or encodeURI method", both complex and difficult to understand, but also through complex programming, really is not to take.

After my test, whether using a new text document and then changing the extension to. js, or using other software or tools to create JS files, using NotePad2 software to open the viewing code, found that JS file encoding is ANSI, so I guess, is the default encoding JS file is ANSI code? Of course, I did not go to textual research, there is no relevant information on the Internet can prove that the default encoding JS file is ANSI code, only individual guess just.

However, at least, if the JS file in Chinese on the page as garbled, may wish to use the method in this article to try, may be able to solve the problem that makes you very headache.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.