The perfect solution JS Chinese character display garbled problem (has solved) _ basic knowledge

Source: Internet
Author: User
The title, just read a few people's discussion, suddenly also think of their recent research issues posted out, and JS in the garbled related problems are sent here, we study together. :)
Scene One: Write the content is utf-8 format, if the saved write data is not utf-8 encoded in Chinese, write nested two times above will have garbled. Since there is no ready-made document on hand, just made a temporary one, this is the 1.htm page code:
Copy Code code as follows:


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<script language= "JavaScript" src= "1.js" ></script>

<body onload=myjs (1) >
</body>

This is the referenced 1.js file code, both files are saved in Simplified Chinese rather than utf-8
Copy Code code as follows:
This is a demonstration program of Chinese character garbled in JS recursive self-writing process
Not only Chinese content is not good, even the annotation if it is Chinese sometimes not
It feels like JS becomes very unstable when it comes to Chinese =_= '
Dec 29th 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 that has run "+i+" Times! ' >\n ';
If the above line is changed to the following sentence can be normal display, it seems that JS support for Chinese characters is not good enough:
t+= "<input Type=button onclick=myjs (" + (i+1) + "value= ' This are my code,it has run" +i+ "times!" >\n ";
t+= "</body>\n";
t+= "
Document.open ();
document.write (t);
Document.close ();
}
Run the 2nd time above there will be garbled.
Scene two: After the XMLHTTP to obtain files and then output, if the file contains Chinese information and GB encoding, display Chinese information will be garbled.
JS code is as follows: (note is the post when added, if afraid of interference can be removed)
Copy Code code as follows:
<script language= "JavaScript" type= "Text/javascript" >
Self.onerror=null;
var Http = new ActiveXObject ("Microsoft.XMLHTTP";
Http.open ("Get", url,false); URLs are the URLs of the pages you need to get
Http.send ();
document.write (Http.responsetext);
</SCRIPT>
Later with the VBS solution, changed 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 need to obtain
Http.send ""
document.write (Bytes2bstr (http.responsebody))
</SCRIPT>
Note: The above bytes2bstr can only responsebody operation, responsetext operation can not be normal display, because ResponseText is the system according to the default encoding processing after the Dongdong, is a pile of garbled, unable to deal with. But this can read the simplified Chinese file, but read utf-8 will be wrong.
Discussion on the related problems of Chinese characters garbled in JS runtime (edit problems please refer to other topics)

First of all thanks to the owner of the advice, this page is good, collection, the specific content of the study
Http://sheneyan.com/test/testAjaxPost.htm

Some of the things I've heard and tested, the key to three puzzles are not solved:

One, even if the page has been normally displayed, if used back or read a Utf-8 page can not be normal after the display;

Second, even if all of the anti-garbled (of course, except for the recoding) measures are used, and the same procedure to open a copper page, also can not guarantee that every time out of the page can be stably displayed as Chinese;

Third, remote access to data submission form of the problem, if the target site with the encoding and decoding, it means that if the display of the normal, the content of the submission became garbled, and vice versa. So I had to encode all the content in advance so that I couldn't get rid of the coding program, and I couldn't escape the shadow =_= of the VBS.

We found a problem in the test.
The previously issued VBS weight-encoding function, if the page is too large, ie will hang off, the specific estimates and the size of the cache and memory relationship. In short, if let me to the degree of Chinese support to order the word js<vbs<asp
The page can be opened to the owner, I Ajax and XML are still relatively unfamiliar, although there have been heard, but has not intervened, can simply introduce, if the forum is not convenient to say, can add me QQ, the information is, thank you!

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.