关于在html中通过.js来调用远程TXT显示

qianlong8 2010-07-22 03:27:44
如提
在一个纯静态的html文件中如何写JS代码来 显示远程的TXT文件
静态html文件为GBK编码
目标TXT文件为中文,范例如下
http://www.jieqi.net/files/article/txt/0/1/3486.txt

只要在HTML中显示TXT内容
框架不用考虑

以下是我正在思考的办法
我自己通过网上搜索的代码 AJAX+XML 来实现
可是xmlResult返回的代码通过 txt00.innerHTML=xmlResult; 输出为乱码。
...全文
630 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxf999 2010-07-26
  • 打赏
  • 举报
回复
显示乱码应该是页面编码不对,试试在提title里加入
<meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
设置编码方式
hoojo 2010-07-22
  • 打赏
  • 举报
回复
设置你的xml编码看看
zoujp_xyz 2010-07-22
  • 打赏
  • 举报
回复
try

<!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>
<title>无标题页</title>

<script type="text/javascript"><!--
//页面级XMLHttp对象
var xmlhttp;

// 向服务器发送信息函数
function getPage(pageURL) {
xmlhttp = createXMLHttp();
if (xmlhttp) {
xmlhttp.onreadystatechange = setPageData;
xmlhttp.open('GET', pageURL);
xmlhttp.send(null);
} else {
alert("XMLHttpRequest对象为空");
}
}
//回调函数,获得从服务器回发的文档信息并显示在 disp层中
function setPageData() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var word = bytes2BSTR(xmlhttp.responseBody);

document.getElementById("disp").innerHTML = word;
}
}
//创建 XMLHttp对象,用于读取远程文档
function createXMLHttp() {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
return new XMLHttpRequest();
} catch (e) {
return null;
}
}
return null;
}
// --></script>

<script language="vbscript" type="text/vbscript">
'VBScritp脚本语言,此函数用于转换读取文档中的乱码
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>

</head>
<body>

<script type="text/javascript">
window.onload = function() { getPage('test.txt'); }
</script>

<div id="disp">
</div>
</body>
</html>

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧