Solve the problem of garbled in the remote page crawl? _javascript Tips

Source: Internet
Author: User
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.
<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.ready state!=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) +" "," V Bscript ") 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].sub String (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 R Eturn unescape (t.join ("%")). Slice (0,-1)} xh.send (null)} </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The transfer code under IE can be improved, and a lot of efficiency can be improved by regular callback process.

Copy Code code as follows:

THIS.BS2STR = function (BS) {
if (!WINDOW.JSMIDB) main.vbinit ("MidB", 2);
if (!WINDOW.JSCHR) main.vbinit ("Chr", 1);
if (!BS2STR.SS) BS2STR.SS = [];
Return unescape (JSMIDB (bs,1)). Replace (/(. { 2}) (. { 2}) (. { 2})/g, "%$3%$2"). Replace (/% ([^0-7].) (.) (. {2}) /g, function (a,a1,a2,a3) {var s=a1+a3;if (!bs2str.ss[s)) BS2STR.SS[S]=JSCHR (eval ("0x" +s)); return bs2str.ss[s];});
}

But I still think it's better to load data with XMLDOM.
Replace (/(. { 2}) (. { 2}) (. { 2})/g, "%$3%$2") to replace (/.{ 2} (. { 2}) (. { 2})/g, "%$2%$1") One less storage. 3 times times faster than the original.
It's more efficient to remove the eval.
Copy Code code as follows:

BYTE () encoding
THIS.BS2STR = function (BS) {
if (!WINDOW.JSMIDB) main.vbinit ("MidB", 2);
if (!WINDOW.JSCHR) main.vbinit ("Chr", 1);
if (!THIS.BS2STR.SS) THIS.BS2STR.SS = [];
Return unescape (JSMIDB (bs,1)). Replace (/.{ 2} (. { 2}) (. { 2})/g, "%$2%$1"). Replace (/% ([^0-7].) (.) (. {2}) /g, function (a,a1,a2,a3) {var s=a1+a3;if (!) This.bs2str.ss[s]) this.bs2str.ss[s]=jschr ("&h" +s); return this.bs2str.ss[s];});
}

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.