网页鼠标抓词中英文单词对译的AJAX小例子(兼容版本)
2007-01-11 10:16
513 查看
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>
/**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>
相关文章推荐
- ajaxfileupload_v.js 解决js版本不兼容,多文件上传不解析json的问题
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- 兼容各浏览器的强制换行,不论中英文。。。。单词,长串等
- AJAX XML 实例 下面的例子将演示网页如何使用 AJAX 来读取来自 XML 文件的信息:
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- [Web开发] IE 所有版本打包下载,网页兼容测试必备
- 兼容早期IE版本的 Ajax 实例
- 网页鼠标指针效果在不同浏览器下兼容
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容
- [Web开发] IE 所有版本打包下载,网页兼容测试必备
- HtmlUnit抓取Ajax网页,例子是去哪儿机票
- poi读写Excel文件,兼容新旧版本的简单例子
- 用ajax获取json数据并在网页上面显示小例子
- 惰性思想处理Ajax低版本IE兼容问题
- 简单的鼠标经过图片时变大,IE低版本浏览器不兼容