asp(JavaScript)自動判斷網(wǎng)頁編碼并轉(zhuǎn)換的代碼
更新時間:2010年06月03日 13:38:21 作者:
asp轉(zhuǎn)換網(wǎng)頁編碼的代碼,用正則匹配頁面的編碼聲明是gb2312還是別的,然后輸出。
完整的示例代碼如下:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaScript自動判斷網(wǎng)頁編碼并轉(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){
//自動判斷編碼開始
var charresult = http_request.ResponseText.match(/CharSet=(\S+)\">/i);
if (charresult != null){
var Cset = charresult[1];
}else{Cset = "gb2312"}//對獲取不到的網(wǎng)站采用gb2312編碼,可自行更改
//自動判斷編碼結(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("http://www.dbjr.com.cn/404.htm"))%>
</body>
</html>
復(fù)制代碼 代碼如下:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaScript自動判斷網(wǎng)頁編碼并轉(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){
//自動判斷編碼開始
var charresult = http_request.ResponseText.match(/CharSet=(\S+)\">/i);
if (charresult != null){
var Cset = charresult[1];
}else{Cset = "gb2312"}//對獲取不到的網(wǎng)站采用gb2312編碼,可自行更改
//自動判斷編碼結(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("http://www.dbjr.com.cn/404.htm"))%>
</body>
</html>
相關(guān)文章
在Vista IIS 7 中用 vs2005 調(diào)試 Web 項(xiàng)目的注意事項(xiàng)
在Vista IIS 7 中用 vs2005 調(diào)試 Web 項(xiàng)目核心是要解決以下幾個問題2008-09-09ScriptHtml 函數(shù)之過濾html標(biāo)記的asp代碼
ScriptHtml 函數(shù)之過濾html標(biāo)記的asp代碼...2007-09-09ASP替換、保存遠(yuǎn)程圖片實(shí)現(xiàn)代碼
這篇文章主要分享了ASP替換、保存遠(yuǎn)程圖片的函數(shù)代碼,需要的朋友可以參考下2015-09-09