The problem solving method of JS Chinese garbled

Source: Internet
Author: User

Sometimes we will encounter the problem of JS Chinese garbled, mainly occurred in the case of a JS file.

When the page contains a JS file in Chinese output, in the page will appear garbled.

Workaround, add charset to the inclusion code, and match the page encoding.

<script charset= "gb2312" src= "/dotbbs_net/js/register.js" language= "Web Effects" ></script>

Analysis

See the Code

1.
2.
3. <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
4. <script type= "Text/javascript" src= "Text.js" ></script>
5.
6. <body>
7.
8. </body>
9.

If there is a statement in Chinese in the text.js, it will produce garbled characters.
In fact, is the second line of CharSet, the second line is mainly set the entire page character encoding way, in this case is UTF-8.
And in JS is a common GB2312 or GBK encoding method.
So will produce garbled.

Workaround:
In <script> this element, our most commonly used attribute is "type" or "language", there is a property is gradually forgotten by us, that is: CharSet. In the above example, we just specify the encoding of JS can be normal display of Chinese. The code is as follows

1.
2.
3. <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
4. <script type= "Text/javascript" src= "Text.js" charset= "GB2312" ></script>
5.
6. <body>
7.
8. </body>
9.

Note The final charset= of the third line, "Gb2312″."

If you use Dreamweaver to write JS, then recommend you to the preferences > New Document "When open an unspecified encoding of existing files to use" tick

Yes, do not save the garbled, a save can not return to the head

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.