Perfectly solves Chinese Character Display garbled characters in JS (resolved)

Source: Internet
Author: User

For example, I have read a few discussions and I suddenly think of posting my recent research questions. All the questions related to garbled code in JS are sent here, we will study it together. :)
Scenario 1: The content written by write is in UTF-8 format. If the saved write data contains Chinese characters not encoded in UTF-8, garbled characters will occur after the write is nested for more than two times. The code for the 1.htm page is as follows:
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Script language = "JavaScript" src = "1.js"> </script>
</Head>
<Body onload = myjs (1)>
</Body>
</Html>
This is the referenced 1. js file code. Both files are saved in simplified Chinese instead of UTF-8.
Copy codeThe Code is as follows: // This is a demo of garbled Chinese characters in the recursive self-writing process of JS.
// Not only does the Chinese content work, but even comments in Chinese sometimes do not work.
// It seems that JS becomes unstable when it encounters a Chinese character = _ ='
// Dec. 29th 2005 created by shouhaimu (QQ: 30836570)
Function myjs (I)
{
Var t = "T + = "T + = "<meta http-equiv = 'content-type' Content = 'text/html; charset = gb2312 '> \ n ";
T + = "<script language = 'javascript 'src = '1. js'> </script> \ n ";
T + = "T + = "<body> \ n ";
T + = "<input type = button onclick = myjs (" + (I + 1) + "value = 'This is a demo program and has run" + I + "times! '> \ N ";
// If the above line is changed to the following sentence, it will be displayed normally. It seems that JS does not have good support for Chinese characters:
// T + = "<input type = button onclick = myjs (" + (I + 1) + "value = 'This is my code, it has run "+ I +" times! '> \ N ";
T + = "</body> \ n ";
T + = "Document. open ();
Document. write (t );
Document. close ();
} Garbled characters may occur when running more than 2nd times.
Scenario 2: When a file is obtained through XMLHTTP and then output, if the file contains Chinese information and is encoded in gb, the Chinese information is garbled during display.
The js Code is as follows: (the comment is added when posting a post. you can remove it if you are afraid of interference)
Copy codeThe Code is as follows: <SCRIPT language = "JavaScript" type = "text/JavaScript">
Self. onerror = null;
Var Http = new ActiveXObject ("Microsoft. XMLHTTP ";
Http. open ("GET", URL, false); // URL is the URL of the page you want to obtain
Http. send ();
Document. write (Http. responseText );
</SCRIPT>
Later, we used VBS to solve the problem and changed it to the following:
<SCRIPT language = "VBScript" type = "text/VBScript">
Function bytes2BSTR (vIn)
StrReturn = ""
For I = 1 To LenB (vIn)
ThisCharCode = AscB (MidB (vIn, I, 1 ))
If ThisCharCode <& H80 Then
StrReturn = strReturn & Chr (ThisCharCode)
Else
NextCharCode = AscB (MidB (vIn, I + 1, 1 ))
StrReturn = strReturn & Chr (CLng (ThisCharCode) * & H100 + CInt (NextCharCode ))
I = I + 1
End If
Next
Bytes2BSTR = strReturn
End Function
Set Http = CreateObject ("Microsoft. XMLHTTP"
Http. open "Get", URL, false // URL is the URL of the page you want to obtain
Http. send ""
Document. write (bytes2BSTR (Http. responseBody ))
</SCRIPT> Note: The above bytes2BSTR operation can only be performed on responseBody. The responseText operation cannot be normally displayed, because responseText is the system's processing result after the default encoding, it's a bunch of garbled characters that cannot be processed. But in this way, you can read files in simplified Chinese, but an error occurs when you read UTF-8.
Discussion on Chinese Character garbled Characters During JS Runtime (for issues arising during editing, refer to other topics)

First of all, thank you for your advice. This page is good and has been added to your favorites. The specific content is under study.
Http://sheneyan.com/test/testAjaxPost.htm

I have also heard of and tested some of them, but there are three questions:

1. Even pages that were previously normally displayed cannot be displayed normally after a backend or a non-UTF-8 page is read;

2. Even if all anti-garbled Code measures (Except re-encoding of course) are adopted, and a page of copper is opened in the same program, it cannot be ensured that every time the pages are displayed in Chinese;

3. Remote Data Acquisition and submission form. If the target website uses encoding and decoding, the submitted content becomes garbled and vice versa. So we had to re-encode all the obtained content in advance, so that we could not do without the encoding program, so we could not get rid of the Shadow = _ = 'of VBS'

A problem was also found in the test just now.
In the previous VBS re-encoding function, if the page is too large, IE will be suspended. The estimated size of the page depends on the cache and memory size. In short, if I have sorted the degree of support for Chinese characters, JS <VBS <ASP <HTML and HTML are all static and of course the most stable, if the client is not required to run, we strongly recommend that you use ASP to generate code.

I am still unfamiliar with Ajax and xml. Although I have heard of it, I have never been involved. Can you briefly introduce it, you can add me QQ. The materials are available. Thank you!

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.