Because the commonly used webpage files are basically in gb2312 encoding mode, if you read them through the xmlhttp Control, garbled characters will certainly occur, but you need to change all the files to UTF-8 encoding, it is difficult to operate. After repeated comparisons, I found a relatively practical solution.
Ff provides the overrideMimeType method to specify the encoding of the returned value. Therefore, it is easy to solve the problem. You only need to point this method to gb2312. For IE, you can use vbs for encoding and gb2utf8 to convert the received binary data. The execScript method is used to call the vbs function.
Copy codeThe Code is as follows:
<Body> </body>
<Script>
LoadPage ("index.htm ")
Function loadPage (url ){
Var xh = window. Event? New XMLHttpRequest (): new ActiveXObject ("Microsoft. XMLHTTP ")
Xh. open ("GET", url, true)
If (window. Event)
Xh. overrideMimeType ("text/xml; charset = gb2312 ")
Xh. onreadystatechange = function (){
If (xh. readyState! = 4)
Return
Var v
V = window. Event? Xh. responseText: window. gb2utf8 (xh. responseBody)
Alert (v)
}
Window. gb2utf8 = function (data ){
Var glbEncode = [], t, I, j, len
Gb2utf8_data = data
ExecScript ("gb2utf8_data = MidB (gb2utf8_data, 1) +'' "," vbscript ")
T = escape (gb2utf8_data ). replace (/% u/g ,""). replace (/(. {2 })(. {2})/g, "%$ 2% $1 "). replace (/% ([A-Z].) % (. {2})/g, "@ $1 $2 ")
T = t. split ("@")
I = 0
Len = t. length
While (++ I <len ){
J = t [I]. substring (0, 4)
If (! GlbEncode [j]) {
Gb2utf8_char = eval ("0x" + j)
ExecScript ("gb2utf8_char = Chr (gb2utf8_char)", "vbscript ")
GlbEncode [j] = escape (gb2utf8_char). substring (1, 6)
}
T [I] = glbEncode [j] + t [I]. substring (4)
}
Gb2utf8_data = gb2utf8_char = null
Return unescape (t. join ("%"). slice (0,-1)
}
Xh. send (null)
}
</Script>