Solve | problem | page
Because the usual use of the Web page files are basically gb2312 encoding, if through the XMLHTTP control read, there will be garbled problems, but to all the files are changed to Utf-8 code, the operation is more troublesome. After repeated comparisons, I found a relatively practical solution.
FF provides a Overridemimetype method that specifies the encoding of the return value, so it is simpler to solve, as long as you point this method at gb2312. For IE, you can use a VBS fit to encode the conversion, using Gb2utf8 to transform the received binary data, which uses the Execscript method to invoke the function of the VBS.
[Copy this Code] CODE:
<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>