asp(JavaScript)自動(dòng)判斷網(wǎng)頁(yè)編碼并轉(zhuǎn)換的代碼
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
asp轉(zhuǎn)換網(wǎng)頁(yè)編碼的代碼,用正則匹配頁(yè)面的編碼聲明是gb2312還是別的,然后輸出。
完整的示例代碼如下: 復(fù)制代碼 代碼如下: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>JavaScript自動(dòng)判斷網(wǎng)頁(yè)編碼并轉(zhuǎn)換</title> </head> <%Server.ScriptTimeout=9999999; function send_request(url){ var codedtext; http_request = Server.CreateObject("Microsoft.XMLHTTP"); http_request.Open("GET",url,false); http_request.Send(null); if (http_request.ReadyState == 4){ //自動(dòng)判斷編碼開始 var charresult = http_request.ResponseText.match(/CharSet=(\S+)\">/i); if (charresult != null){ var Cset = charresult[1]; }else{Cset = "gb2312"}//對(duì)獲取不到的網(wǎng)站采用gb2312編碼,可自行更改 //自動(dòng)判斷編碼結(jié)束 codedtext = bytesToBSTR(http_request.Responsebody,Cset); }else{ codedtext = "Erro"; } return(codedtext); } function bytesToBSTR(body,Cset){ var objstream; objstream = Server.CreateObject("Adodb.Stream"); objstream.Type = 1; objstream.Mode = 3; objstream.Open(); objstream.Write(body); objstream.Position = 0; objstream.Type = 2; objstream.Charset = Cset; bytesToBSTR = objstream.Readtext; objstream.Close; return(bytesToBSTR); }%> <body> <%Response.Write(send_request("> </body> </html> 該文章在 2010/11/26 0:41:53 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |