网页鼠标抓词中英文单词对译的AJAX小例子(兼容版本)

translater.js

/**Translater
 * @author Robin Chen Email:Robchen@126.com MSN:favodesigner@hotmail.com QQ:4705648
 * prototype.js needed
 */
 
var Translater = { 
 init : function(){
  var content = "<div id=/"translater_result/" style=/"display:none;position:absolute;background-color:white;border:1px black solid;z-index:1000/">翻译结果如下:<br/><hr size=/"1/" noshoade/><span id=/"translater_content/" style=/"color:red/"></span></div>"
  document.write(content);
 },
 translateModeOn : function(){
  this.containter = $("translater_result");
  this.content = $("translater_content");
  this.moveMode = true;
  this.translateMode = true;
  Event.observe(document,"mousemove",this.doMove,false);
  Event.observe(document,"mouseup",this.translate,false);
 },
 translateModeOff : function(){
  this.moveMode = false;
  this.translateMode = false;
  Event.stopObserving(document,"mousemove",this.doMove,false);
  Event.stopObserving(document,"mouseup",this.translate,false); 
 },
 doMove : function(evt){ 
  var evt = evt || event;
  if(this.moveMode == false)return;
  Translater.containter.style.left = evt.clientX + 10 + "px";
  Translater.containter.style.top = evt.clientY + 10 + "px";
 },
 translate : function(){
  if(this.translateMode == false)return;
  if(document.all){
   var content = document.selection.createRange();
  }else{
   var content = window.getSelection();
   content.text = content.toString();
  }
  if(content.text.length == 0){
   Element.hide(Translater.containter);
   return;
  }
  if(Translater.searchWord == null){
  Translater.searchWord = new Ajax.Request(
   "test.asp",//for example
   {
   method:"get",
   parameters:"word="+escape(content.text),
   onLoading:function(){
    Translater.content.innerHTML = "loading";
    Element.show(Translater.containter);
   },
   onComplete:function(x){
    Translater.content.innerHTML = x.responseText;
    Translater.searchWord = null;
    var imgs = Translater.content.getElementsByTagName("img");
    (parseInt(imgs.length)).times(function(i){
     imgs[i].src = imgs[i].src.replace(document.domain,"www.baidu.com");
    });
    }
   });
  }
 },
 containter : null,
 content : null,
 translateMode : false,
 moveMode : false,
 searchWord : null
}
Translater.init();
Translater.translateModeOn();

test.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<style type="text/css">
<!--
body {
 font-size:9pt;
 color:black;
}
#textBox {
 position:absolute;
 width:403px;
 height:285px;
 z-index:1;
 top: 63px;
 border:1px black solid;
}
-->
</style>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="translater.js"></script>
</head>

<body>
<div id="textBox">
While the other coats drooped, this one looked as if it were holding itself up. The thick, black wool of the double-breasted chesterfield was soft and unworn, as though it had been preserved in mothballs for years in dead old Uncle Henry's steamer trunk. The coat had a black velvet collar, beautiful tailoring, a Fifth Avenue label and an unbelievable price of $28. We looked at each other, saying nothing, but John's eyes gleamed. Dark, woolen topcoats were popular just then with teenage boys, but could cost several hundred dollars new. This coat was even better, bearing that touch of classic elegance from a bygone era.
</div>
<div id="topic">用鼠标选择中文或者英文词语后自动显示翻译结果:</div>
</body>
</html>

prototype.js略

test.asp

< %@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
<script language="javascript" runat="server">
Response.Charset = "gb2312";
var word = Request("word");
if(word == "")Response.End();
var url = " http://www.baidu.com/baidu?ie=gb2312&bs=&sr=&z=&ct=1048576&cl=3&f=8&word=" + Server.URLEncode(word);
var xmlhttp = Server.createObject("Microsoft.XMLHTTP")
 xmlhttp.open("GET",url,false);
 xmlhttp.send();
if(xmlhttp.readyState == 4){
 var responseBack = BytesToBstr(xmlhttp.responseBody,"gb2312");
 responseBack = responseBack.replace(/^(/n|/r|.)*(<ol>(/n|/r|.)*<//ol>)(/n|/r|.)*$/i,"$2");
 Response.Write("<span style=/"font-size:9pt/">" + responseBack + "<//span>");
 Response.End();
}
</script>
<script language="vbscript" runat="server">
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
</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值