小巧适用的AJAX英汉汉英字典
俺们程序员在工作中常常会查查字典,找一下英文单词怎么拼的或者看看单词的意思。这个时候我们必然要使用一些字典软件,如金山词霸之类,或者去一些查单词的网站,到达查询页面后,输入单词,等待刷新之后才能获得查询的结果。
这里送给大家一款AJAX的英汉汉英字典,体积小、纯HTML打造、无需安装、反应速度快、方便快捷、功能强大,是居家旅行、杀人放火之必备良药。那么,请问这么好的字典,哪里有买的呢?且看:
<html>
<meta http-equiv=Content-Type content="text/html;charset=gb2312">
<head>
<link href="http://www.dict.cn/img/search.css" rel="stylesheet" type="text/css">
<script src='http://www.dict.cn/img/search.js' language='javascript'></script>
<script language="javascript">
window.onload = function()
{
}
var xmlHttp = false;
var e;
//创建XMLHTTP对象
function getXMLHTTPObj()
{
var C = null;
try
{
C = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
C = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(sc)
{
C = null;
}
}
if( !C && typeof XMLHttpRequest != "undefined" )
{
C = new XMLHttpRequest();
}
return C;
}
//调用远程方法
function callServer(e)
{
document.getElementById("lookup_result").style.display = "none";
//获取查询日期
var lookup_obj = document.getElementById("word");
var lookup_word = lookup_obj.value;
LookUp(lookup_word);
}
//查字典
function LookUp(sIn)
{
var lookup_obj = document.getElementById("word");
lookup_obj.value = sIn.split("&")[0];
try
{
if( xmlHttp && ( xmlHttp.readyState != 0 ) )
{
xmlHttp.abort();
}
xmlHttp = getXMLHTTPObj();
if( xmlHttp )
{
//构造查询连接字符串
var url = "http://www.dict.cn/search/?q=" + sIn;
//打开连接
xmlHttp.open("GET", url, true);
//设置回调函数
xmlHttp.onreadystatechange = updatePage;
//发送请求
xmlHttp.send(null);
}
else
{
document.getElementById("flag").innerHTML = "XMLHTTP对象创建失败";
}
}
catch (e)
{
document.getElementById("flag").innerHTML = "查询错误:" + e;
}
}
//回调处理函数
function updatePage()
{
try
{
if (xmlHttp.readyState == 1)
{
// document.getElementById("flag").innerHTML = "正在加载连接对象";
}
if (xmlHttp.readyState == 2)
{
// document.getElementById("flag").innerHTML = "连接对象加载完毕。";
}
if (xmlHttp.readyState == 3)
{
// document.getElementById("flag").innerHTML = "数据获取中";
}
if (xmlHttp.readyState == 4)
{
// document.getElementById("flag").innerHTML = "数据获取成功";
var response = bytes2BSTR(xmlHttp.responseBody) ;
var OpenValue = response.split("<big><b>")[1];
var OpenValue = OpenValue.split("<td align=\"right\">")[0];
var OpenValue = ReplaceAll(OpenValue,"/img/","http://www.dict.cn/img/");
//替换 近似解释 的查询链
var OpenValue = ReplaceAll(OpenValue,"href=./?q=","href=javascript:LookUp(\"");
var OpenValue = ReplaceAll(OpenValue,"><<font color=#223322","\")><<font color=#223322");
//替换 同义词 的查询链接
var restr = /<a href="index.php\?q=(.*?)">(.*?)<\/a>/g;
var newstr = "<a href=\"javascript:LookUp('$1')\">$2</a>";
var OpenValue = OpenValue.replace(restr,newstr);
//屏蔽 添加到单词本
var OpenValue = ReplaceAll(OpenValue,"src=http://www.dict.cn/img/wordbook.gif","src=/img/wordbook.gif width=0");
var OpenValue = ReplaceAll(OpenValue,"加入生词本","");
document.getElementById("flag").innerHTML = OpenValue;
}
}
catch (e)
{
document.getElementById("flag").innerHTML = "回调处理错误:" + e;
}
}
function enterToTab()
{
if(event.keyCode == 13)
{
event.keyCode = 9;
}
}
</script>
<script type="text/vbscript" language="vbscript">
Function ReplaceAll(sIn,sFind,sReplace)
strReturn = ""
strReturn = Replace(sIn,sFind,sReplace)
ReplaceAll = strReturn
End Function
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>
<form>
<span class="f pl30">请输入要查询的汉字或者英文:</span><br/>
<input type="text" id="word" onblur="callServer(this);" onkeydown="enterToTab();" onmouseover="this.focus()" onfocus="this.select()" value="english" accesskey="c" />
<div id="lookup_result">
</div>
<div id="flag">
</div>
</form>
</body>
</html>
<meta http-equiv=Content-Type content="text/html;charset=gb2312">
<head>
<link href="http://www.dict.cn/img/search.css" rel="stylesheet" type="text/css">
<script src='http://www.dict.cn/img/search.js' language='javascript'></script>
<script language="javascript">
window.onload = function()
{
}
var xmlHttp = false;
var e;
//创建XMLHTTP对象
function getXMLHTTPObj()
{
var C = null;
try
{
C = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
C = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(sc)
{
C = null;
}
}
if( !C && typeof XMLHttpRequest != "undefined" )
{
C = new XMLHttpRequest();
}
return C;
}
//调用远程方法
function callServer(e)
{
document.getElementById("lookup_result").style.display = "none";
//获取查询日期
var lookup_obj = document.getElementById("word");
var lookup_word = lookup_obj.value;
LookUp(lookup_word);
}
//查字典
function LookUp(sIn)
{
var lookup_obj = document.getElementById("word");
lookup_obj.value = sIn.split("&")[0];
try
{
if( xmlHttp && ( xmlHttp.readyState != 0 ) )
{
xmlHttp.abort();
}
xmlHttp = getXMLHTTPObj();
if( xmlHttp )
{
//构造查询连接字符串
var url = "http://www.dict.cn/search/?q=" + sIn;
//打开连接
xmlHttp.open("GET", url, true);
//设置回调函数
xmlHttp.onreadystatechange = updatePage;
//发送请求
xmlHttp.send(null);
}
else
{
document.getElementById("flag").innerHTML = "XMLHTTP对象创建失败";
}
}
catch (e)
{
document.getElementById("flag").innerHTML = "查询错误:" + e;
}
}
//回调处理函数
function updatePage()
{
try
{
if (xmlHttp.readyState == 1)
{
// document.getElementById("flag").innerHTML = "正在加载连接对象";
}
if (xmlHttp.readyState == 2)
{
// document.getElementById("flag").innerHTML = "连接对象加载完毕。";
}
if (xmlHttp.readyState == 3)
{
// document.getElementById("flag").innerHTML = "数据获取中";
}
if (xmlHttp.readyState == 4)
{
// document.getElementById("flag").innerHTML = "数据获取成功";
var response = bytes2BSTR(xmlHttp.responseBody) ;
var OpenValue = response.split("<big><b>")[1];
var OpenValue = OpenValue.split("<td align=\"right\">")[0];
var OpenValue = ReplaceAll(OpenValue,"/img/","http://www.dict.cn/img/");
//替换 近似解释 的查询链
var OpenValue = ReplaceAll(OpenValue,"href=./?q=","href=javascript:LookUp(\"");
var OpenValue = ReplaceAll(OpenValue,"><<font color=#223322","\")><<font color=#223322");
//替换 同义词 的查询链接
var restr = /<a href="index.php\?q=(.*?)">(.*?)<\/a>/g;
var newstr = "<a href=\"javascript:LookUp('$1')\">$2</a>";
var OpenValue = OpenValue.replace(restr,newstr);
//屏蔽 添加到单词本
var OpenValue = ReplaceAll(OpenValue,"src=http://www.dict.cn/img/wordbook.gif","src=/img/wordbook.gif width=0");
var OpenValue = ReplaceAll(OpenValue,"加入生词本","");
document.getElementById("flag").innerHTML = OpenValue;
}
}
catch (e)
{
document.getElementById("flag").innerHTML = "回调处理错误:" + e;
}
}
function enterToTab()
{
if(event.keyCode == 13)
{
event.keyCode = 9;
}
}
</script>
<script type="text/vbscript" language="vbscript">
Function ReplaceAll(sIn,sFind,sReplace)
strReturn = ""
strReturn = Replace(sIn,sFind,sReplace)
ReplaceAll = strReturn
End Function
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>
<form>
<span class="f pl30">请输入要查询的汉字或者英文:</span><br/>
<input type="text" id="word" onblur="callServer(this);" onkeydown="enterToTab();" onmouseover="this.focus()" onfocus="this.select()" value="english" accesskey="c" />
<div id="lookup_result">
</div>
<div id="flag">
</div>
</form>
</body>
</html>
拷贝以上代码,另存为一个html的文件,字典便有了。
界面相当的简单,只有一个输入框,填入你想要查询的单词,Tab或者回车一下,单词解释会立马显示出来。汉语的词有相近单词;英语的话有近似词,均可直接点击查询哦。
最后再次感谢一下我们的字典数据来源: www.dict.cn ,网站的确做的不错,是个学英语的好去处。