Reference external js garbled problem analysis and solution _ javascript skills

Source: Internet
Author: User
Javascript will inevitably be used in web development. To make the page slim, we usually put the js Code outside and reference it through src, if the encoding used by the web page and js is different, garbled code will be generated. Next, let's share the solution with you, if you are interested, refer to the following: In web development, we generally use js inevitably. We can place js Code directly on the page to use js internally. But in order to make the page "slim", we usually put the js Code outside and reference it through src. At this time, we need to pay attention to the encoding problem. If JavaScript code is used for web pages, garbled characters may occur. (You do not need to pay attention to encoding when using js internally, because they use the same encoding)

For most web pages, we generally use the following two types of encoding: UTF-8, GB2312. Therefore, we only need to agree to the page and js Code to solve the garbled problem:
For GBK page reference code for UTF-8-Encoded JavaScript files if garbled problem, you can use the following code to solve

The Code is as follows:


<Script type = "text/javascript" src = "test. js" charset = "UTF-8"> script


Introduce the garbled JavaScript file encoded as gbk in the UTF-8 page, you can use the following method:

The Code is as follows:


<Script type = "text/javascript" src = "test. js" charset = "gb2312"> script


This method is for the js file appears in Chinese characters, and other easily caused in GBK and UTF-8 Encoding Error characters when using, for pure English do not need. If you want insurance, you can add ....
Related Article

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.