


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)!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
