How to solve the garbled problem in remote page capture

Source: Internet
Author: User

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>

Related Article

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.