pxysea 发表于 2013-2-7 15:27:34

Ajax 编码转换,解决乱码问题

Ajax 编码转换,解决乱码问题<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>xml Http</title><script language=vbscript>Function rsB(vIn) rsB=MidB(vIn,1)End FunctionFunction vbChar(ss) vbChar = Chr(ss)End Function</script><script language=javascript>//测试用的网址url="http://www.hnnw.net/file/public/weather.htm";//**全局变量**//glbEncode储存ascii到unicode字符的转换,这样做的好处是可以提高重复解码时的解码效率glbEncode=new Array();//用同步方式获取网页内容function loadURL(strURL){ if(!strURL){return(false);} var xmlHTTP=new ActiveXObject("MSXML2.XMLHTTP"); xmlHTTP.open("GET",strURL,false); xmlHTTP.send(""); return xmlHTTP;}//重编码function reCode(b){ var t=rsB(b); t=escape(t).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%(.)%(.{2})/g,"@$1$2"); t=t.split("@"); var i=0,j=t.length,k; while(++i<j) {k=t.substring(0,4);if(!glbEncode)glbEncode=escape(vbChar(eval("0x"+k))).substring(1,6);t=glbEncode+t.substring(4); } return unescape(t.join("%"));}function init(){ stTime1=(new Date()).getTime(); ree=loadURL(url).responseBody; stTime2=(new Date()).getTime(); s001=reCode(ree); stTime3=(new Date()).getTime(); document.body.innerHTML="共计"+s001.length+"字节\n下载耗时:"+(stTime2-stTime1)+"\n解码耗时:"+(stTime3-stTime2)+"\n"+s001+"";}</script></head><body onload=init()>下载中...</body></html> 
页: [1]
查看完整版本: Ajax 编码转换,解决乱码问题