当前位置:编程学习 > asp >>

XMLHTTP打造漂亮实用的在线桌面翻译

答案: 

最近常看e文文档,只怪当时读书不努力,现在知道吃力了,经常打开Kingsoft的词霸搜索网站来翻译,搞的很麻烦,无聊之际想到了搞个[桌面翻译]。如果能直接在桌面上输入要翻译的词然后就可以返回结果那应该方便许多吧,嘿嘿。说干就干。

怎么弄呢?在桌面属性里有一个WEB选项,允许你的桌面用当前主页或其他的网页来显示。我们不如就做个网页好了。开始想着直接写一个提交表单的页就算了然后打开新页来看结果,后来想想这样也不是很好,还要自己翻,不能把最主要的东西一下直接的显示出来,所以就有了[偷]结果的想法。

[偷]结果我们常用的就是用XMLHTTP的方式来请求数据啦,这样的话我们基本任务就已经确定下来了:

写一个提交页面,再写一个截取结果的页面。

本来是说用javascript来完成所有操作,但目标站点的数据采用的是UTF-8编码,取回来的数据一下都成了乱码,没办法,头都大了,只好换ASP了。

提交页面iciba.htm

<html>
<head>
<script language="javascript">
function view(){
 var s = document.all.s.value;
 var l = document.all.lang.value;
 var t = document.all.t.value;
 var cs = "s=" + s + "&lang=" + l + "&t=" + t;
 var url = "http://localhost/xml/result.asp?"
 top.ShowResult.location = url + cs;
 return false;
}
</script>
</head>
<body bgcolor="#3A6EA5" leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" scroll="no" oncontextmenu="return false">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
 <tr>
  <td>&nbsp;</td>
  <td width="250" height="100%" align="center" valign="bottom">
   <table width="250" height="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
 <td>
  <iframe id="ShowResult" name="ShowResult" style="background:#3A6EA5;" width="250" height="100%" frameborder="0" src="http://localhost/xml/result.asp"></iframe>
 </td>
    </tr>
    <tr>
 <td height="50" align="center" style="FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr='#3A6EA5',endColorStr='#3A6EA5');">
  <form name="kf" id="form">
         <input name="s" id="word" size="25" style="font-size:12px" maxlength="255" type="text" onkeydown="if(event.keyCode==13){return view();}" />
         <input type="hidden" name="lang" value="utf-8" />
         <input type="hidden" name="t" value="word" id="t" />
         <input type="button" style="font-size:12px" value="翻译" onclick="return view();" />
      </form>
 </td>
    </tr>
   </table>
  </td>
 </tr>
</table>
</body>
</html>

截取数据页面result.asp

<html>
<head>
<script language="javascript" type="text/javascript">
function str2img(str)
{
    var lenStr = str.length;
    var rsString    = "";
    for (var i=0;i<lenStr;i++ ) {
        var theChar   = str.substr(i,1);
        if (theChar == " "){
            rsString += " ";}
        else if (theChar == "-"){
            rsString += "<img src=\"ui/i/yinbiao/zhonggangxian.png\" border=\"0\" align=absmiddle>";}                       
        else if (theChar == "_"){
            rsString += "<img src=\"ui/i/yinbiao/xiahuaxian.png\" border=\"0\" align=absmiddle>";}           
        else if (theChar == "."){
            rsString += "<img src=\"ui/i/yinbiao/dian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "\\"){
            rsString += "<img src=\"ui/i/yinbiao/xiexian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "/"){
            rsString += "<img src=\"ui/i/yinbiao/fanxiexian.png\" border=\"0\" align=absmiddle>";}
        else if (theChar == "?"){
            rsString += "<img src=\"ui/i/yinbiao/wenhao.png\" border=\"0\" align=absmiddle>";}
        else{
            rsString += "<img src=\"ui/i/yinbiao/"+theChar+".png\" border=\"0\" align=absmiddle>";}
    }
    document.write(rsString);
}
</script>
<base href=>http://cb.kingsoft.com/">
<LINK href=><style>
body{font-size:12px;background-color:#3A6EA5;}
</style>
</head>
<body topmargin="0" bottommargin="0">
<%
on error resume next

function getHTTPPage(url)
   dim Http
   set http=createobject("Microsoft.XMLHTTP")
   Http.open "GET",url,false
   Http.send()
   if Http.readystate<>4 then
   exit function
   end if
   getHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8")
   set http=nothing
   if err.number<>0 then err.Clear
end function
Function BytesToBstr(body,Cset)
 dim objstream
 set 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
 set objstream = nothing
End Function
Function toUTF8(szInput)
    Dim wch, uch, szRet
    Dim x
    Dim nAsc, nAsc2, nAsc3
   
    If szInput = "" Then
        toUTF8 = szInput
        Exit Function
    End If
    For x = 1 To Len(szInput)
        wch = Mid(szInput, x, 1)
        nAsc = AscW(wch)
        If nAsc < 0 Then nAsc = nAsc + 65536
   
        If (nAsc And &HFF80) = 0 Then
            szRet = szRet & wch
        Else
            If (nAsc And &HF000) = 0 Then
                uch = "%" &am

上一个:xmldom的一个小用法
下一个:实例演练ASP+XML编程(四)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,