DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XMLHTTP打造漂亮實用的在線桌面翻譯
XMLHTTP打造漂亮實用的在線桌面翻譯
編輯:XML詳解     

怎麼弄呢?在桌面屬性裡有一個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="/ui/c/main.css" type="text/CSS" rel="stylesheet">
<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 = "%" & Hex(((nAsc 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
        szRet = szRet & uch
      Else
        uch = "%" & Hex((nAsc 2 ^ 12) Or &HE0) & "%" & _
              Hex((nAsc 2 ^ 6) And &H3F Or &H80) & "%" & _
              Hex(nAsc And &H3F Or &H80)
        szRet = szRet & uch
      End If
    End If
  Next
    
  toUTF8 = szRet
End Function
  for each i in request.querystring
cs = cs & "&" & i & "="
If instr(cs,"s")<1 then
 cs = cs & request.querystring(i)
else
 cs = cs & toUTF8(request.querystring(i))
end if
Next
  cs = right(cs,len(cs)-1)
  url = "http://cb.kingsoft.com/search?" & cs
'response.write url
'response.end
  If url<>"" then
  str = getHTTPPage(url)
  ls = instr(str,"<div style=""background: #F6F6F6")-15
  rs = instr(str,"</div><p")+6
  ns = rs-ls
  resultstr = mid(str,ls,ns)
  resultstr = replace(resultstr,"./search?","http://localhost/XML/result.ASP?")
  Response.write resultstr
End if
  %>
  </body>
</Html>

  因為iciba.htm要放到桌面上如果直接查找文件選擇它的話就不能用HTTP訪問,所以裡邊用了result.ASP的絕對路徑,當然如果大家在桌面上選擇網頁的時候選擇地址的話即用HTTP訪問的話可以把那個地址改成相對路徑。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved