欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp(JavaScript)自动判断网页编码并转换的代码

程序员文章站 2022-06-05 09:09:45
完整的示例代码如下: 复制代码 代码如下: <%@language="javascript" codepage="65001"%> &...
完整的示例代码如下:
复制代码 代码如下:

<%@language="javascript" codepage="65001"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>javascript自动判断网页编码并转换</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"}//对获取不到的网站采用gb2312编码,可自行更改
//自动判断编码结束
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("//www.jb51.net/404.htm"))%>
</body>
</html>