Home >Web Front-end >JS Tutorial >Perfectly solve the problem of Chinese characters displaying garbled characters in JS (solved)_Basic knowledge
As mentioned, I just read a few discussions and suddenly thought of posting the issues I have been researching recently. All issues related to garbled characters in JS are posted here so that we can study them together. . :)
Scenario 1: The content written by write is in UTF-8 format. If the saved write data contains Chinese that is not UTF-8 encoded, nesting the write more than twice will cause garbled characters. Since there is no ready-made file at hand, I just made a temporary one. This is the code of the 1.htm page:
The code is as follows:
<html> <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 quote’s 1.js file code, both files are saved in simplified Chinese instead of utf-8
The code is as follows:
//这是JS 递归 自写过程中汉字乱码的演示程序 //不光是中文内容不行,就连 注释 如果是中文有时候也不行 //感觉JS一碰上中文就变得很不稳定=_=' //Dec. 29th 2005 created by shouhaimu(QQ:30836570) function myjs(i) { var t="<html>\n"; t+="<head>\n"; t+="<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>\n"; t+="<script language='JavaScript' src='1.js'></script>\n"; t+="</head>\n"; t+="<body>\n"; t+="<input type=button onclick =myjs("+(i+1)+" value='这是演示程序,已经运行了"+i+"次!'>\n"; //上面这行如果改成下面这句就可以正常显示,看来确实是JS对汉字的支持不够好: //t+="<input type=button onclick=myjs("+(i+1)+" value='This is my code,it has run "+i+" time s!'>\n"; t+="</body>\n"; t+="</html>\n"; document .open(); document.write(t); document.close(); }
It will run for the second time or more There are garbled characters.
Scenario 2: When a file is obtained through XMLHTTP and then output, if the file contains Chinese information and is encoded by gb, the Chinese information will be garbled when displayed.
The js code is as follows: (comments were added when posting, you can remove them if you are afraid of interference)
The code is as follows:
<SCRIPT language="JavaScript" type="text/JavaScript"> self.onerror= null ; var Http = new ActiveX Object ("Microsoft.XMLHTTP"; Http.open("GET",URL,false); //URL是您需要获取的页面的网址 Http.s end (); document.write(Http.responseText); </SCRIPT> 后来用VBS解决,改成如下: <SCRIPT language=" VBScript " type="text/VBScript"> Function bytes2BSTR(vIn) str Return = "" For i = 1 To LenB(vIn) ThisCharCode = AscB(MidB(vIn,i,1)) If ThisCharCode < &H80 Then strReturn = strReturn & Chr (ThisCharCode) Else Next CharCode = 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是您需要获取的页面的网址 Http.send "" document.write(bytes2BSTR(Http.responseBody)) </SCRIPT>
Note: The above bytes2BSTR only The responseBody can be operated, but the responseText operation cannot be displayed normally, because the responseText is processed by the system according to the default encoding. It is a bunch of garbled code and cannot be processed. But this way I can read Simplified Chinese files, but there will be errors when reading UTF-8.
Discussion on issues related to garbled Chinese characters when JS is running (please refer to other special posts for problems that arise during editing)
I have heard about some of them and done tests. There are still three key mysteries that have not been solved. :
1. Even if the page was displayed normally, it will no longer be displayed normally if you use back or read a non-utf-8 page;
2. Even if all the protection measures are taken, All measures for garbled characters (except for re-encoding, of course) have been adopted, and even if you use the same program to open a page, there is no guarantee that the page will be stably displayed in Chinese every time;
3. Remote acquisition of data submission The problem with the form is that if the target website uses encoding and decoding, it means that if the display is normal, the submitted content will become garbled, and vice versa. So I had to re-encode all the obtained content in advance, so I couldn't do without the encoding program, and I couldn't get rid of the shadow of VBS =_='
I also found a problem in the test just now
Posted previously VBS re-encoding function , if the page is too large, IE will hang. The specific size is estimated to be related to the cache and memory size. In short, if I were to rank the degree of Chinese support, JS
The above is the perfect solution to the problem of garbled Chinese character display in JS (solved)_basic knowledge. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!