javascript 多种搜索引擎集成的页面实现代码


Posted in Javascript onJanuary 02, 2010

- 输入一个关键词,鼠标点击后面的搜索引擎链接,即可进入到该引擎的页面
- 如果输入关键词后敲回车,则使用默认搜索引擎,而每选择新的搜索引擎,默认引擎也会随之改变
- 自动记忆上次使用的搜索引擎,后面添加*
javascript 多种搜索引擎集成的页面实现代码
源代码如下,使用了多种IE/FF的适应办法:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<style type="text/css"> 
<!-- 
.normal {font-family: "Verdana","Arial";font-size:14px} 
.small {font-family: "Verdana","Arial";font-size:12px} 
body { font-family: "Verdana","Arial";font-size:14px} 
td {font-family: "Verdana","Arial";font-size:14px} 
th {font-family: "Verdana","Arial";font-size:15px} 
input {font-family: "Verdana","Arial";font-size:14px} 
a {text-decoration:underline; color:blue} 
a:link { text-decoration:underline; color: blue } 
a:visited { text-decoration:underline; color: blue } 
a:hover { text-decoration: underline; color: red } 
--> 
</style> 
<title>My Portal</title> 
<script language="javascript"> 
// 这一段使得FireFox也支持IE的innerText方法 
function isIE(){ 
if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
return true; 
else 
return false; 
} 
if(!isIE()){ //firefox innerText define 
HTMLElement.prototype.__defineGetter__( "innerText", 
function(){ 
var anyString = ""; 
var childS = this.childNodes; 
for(var i=0; i <childS.length; i++) { 
if(childS[i].nodeType==1) 
anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText; 
else if(childS[i].nodeType==3) 
anyString += childS[i].nodeValue; 
} 
return anyString; 
} 
); 
HTMLElement.prototype.__defineSetter__( "innerText", 
function(sText){ 
this.textContent=sText; 
} 
); 
} 
// 这一段使得FireFox的HTMLElement具有click方法(add click method to HTMLElement in Mozilla) 
try { 
// create span element so that HTMLElement is accessible 
document.createElement('span'); 
HTMLElement.prototype.click = function () { 
if (typeof this.onclick == 'function') 
this.onclick({type: 'click'}); 
}; 
} 
catch (e) { 
// alert('click method for HTMLElement couldn\'t be added'); 
} 
// 对HTMLAnchorElement 加入onclick事件 
try { 
// create a element so that HTMLAnchorElement is accessible 
document.createElement('a'); 
HTMLElement.prototype.click = function () { 
if (typeof this.onclick == 'function') { 
if (this.onclick({type: 'click'}) && this.href) 
window.open(this.href, this.target? this.target : '_self'); 
} 
else if (this.href) 
window.open(this.href, this.target? this.target : '_self'); 
}; 
} 
catch (e) { 
// alert('click method for HTMLAnchorElement couldn\'t be added'); 
} 
// 跟踪回车键事件 
function captureKeys (evt) { 
var keyCode = evt.keyCode ? evt.keyCode : 
evt.charCode ? evt.charCode : evt.which; 
if (keyCode == 13) { 
// cancel key: 
if (evt.preventDefault) { 
evt.preventDefault(); 
} 
var dq = getCookie('default-engine'); 
if( dq == null) dq = "baidu_txt"; 
submit_query( dq ); 
return false; 
} 
return true; 
} 
// cookie 功能函数 
function getCookie(cookieName) 
{ 
var cookieString = document.cookie; 
var start =cookieString.indexOf(cookieName+'='); 
if(start == -1) 
return null; 
start += cookieName.length + 1; 
var end = cookieString.indexOf(';', start); 
if(end == -1) 
return unescape(cookieString.substring(start)); 
return unescape(cookieString.substring(start,end)); 
} 
function setCookie(cookieName, cookieValue) 
{ 
var expires = new Date(); 
expires.setTime( expires.getTime() + 3*30*24*60*60*1000); // 3 months 
document.cookie = cookieName + '=' + escape(cookieValue)+';expires='+expires.toGMTString(); 
} 
function removeCookie(cookieName) 
{ 
var expires = new Date(); 
expires.setTime( expires.getTime() - 1); 
document.cookie = cookieName + '=fooxxx;expires='+expires.toGMTString(); 
} 
function $(id) { 
return document.getElementById(id); 
} 
// 调式Object用,适用于IE,Firefox下可用firebug 
function dumpObject(obj) 
{ 
var temp=""; 
for (x in obj) 
temp += x + ": " + obj[x] + "\n"; 
var popup = window.createPopup(); 
popup.document.body.innerHTML = '<textarea rows=30 cols=40>' + temp + '</textarea>'; 
popup.show(100, 100, 300, 400, document.body); 
} 
// 多种查询引擎请求分派 
function submit_query(t_query) 
{ 
var keyword = document.getElementById("keyword"); 
var mylink = document.getElementById("mylink"); 
var loc = ""; 
switch(t_query) 
{ 
case "baidu_txt": 
loc = "http://www.baidu.com/s?wd=" + keyword.value; 
break; 
case "baidu_img": 
loc = "http://image.baidu.com/i?ct=201326592&lm=-1&tn=baiduimagenojs&pv=&word=" + keyword.value + "&z=0&pn=0&rn=16&cl=2"; 
break; 
case "yodao_txt": 
loc = "http://www.yodao.com/search?q=" + encodeURI(keyword.value) + "&ue=utf8&keyfrom=web.index"; 
break; 
case "yodao_img": 
loc = "http://image.yodao.com/search?q=" + encodeURI(keyword.value) + "&ue=utf8&keyfrom=image.index"; 
break; 
case "yodao_dict": 
loc = "http://dict.yodao.com/search?q=" + encodeURI(keyword.value) + "&ue=utf8&keyfrom=dict.index"; 
break; 
case "yodao_blog": 
loc = "http://blog.yodao.com/search?q=" + encodeURI(keyword.value) + "&ue=utf8&keyfrom=blog.top"; 
break; 
case "iask_ditu": 
loc = "http://ditu.iask.com/a/r.php?cl=北京&key=" + keyword.value; 
break; 
case "verycd": 
loc = "http://www.verycd.com/search/folders/" + encodeURI(keyword.value); 
break; 
case "google_txt": 
loc = encodeURI("http://www.google.com/search?hl=en&q=" + keyword.value +"&btnG=Google+Search"); 
break; 
case "yahoo_txt": 
loc = "http://search.yahoo.com/search?p="+encodeURI(keyword.value)+"&vc=&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8&fp_ip=CN"; 
break; 
case "trans_en": 
loc = "http://209.85.171.104/translate_c?hl=en&sl=en&tl=zh-CN&u=http://" + keyword.value + "/&usg=ALkJrhijPevZUxPtrHDj94k1U9Wo8x7S8g"; 
break; 
case "google_code": 
loc = "http://www.google.com/codesearch?q=" + keyword.value + "&hl=en&btnG=Search+Code"; 
break; 
case "google_scholar": 
loc = "http://scholar.google.com/scholar?q=" + keyword.value + "&hl=en&lr=&btnG=Search"; 
break; 
} 
setEngine(t_query); 
setCookie('default-engine', t_query); 
mylink.href= loc; 
mylink.click(); 
} 
function getDefaultEngine() 
{ 
var dq = getCookie('default-engine'); 
if( dq == null) dq = "baidu_txt"; 
return dq; 
} 
function setDefaultEngine() 
{ 
var old_e = getDefaultEngine(); 
if( $(old_e).innerText.indexOf('*') < 0) 
$(old_e).innerText = $(old_e).innerText + '*'; 
} 
function setEngine( new_e ) 
{ 
var old_e = getDefaultEngine(); 
if( $(old_e).innerText.indexOf('*') >= 0) 
$(old_e).innerText = $(old_e).innerText.replace('*',''); 
if( $(new_e).innerText.indexOf('*') < 0) 
$(new_e).innerText = $(new_e).innerText + '*'; 
setCookie('default-engine', new_e); 
} 
</script> 
</head> 
<body onload='setDefaultEngine()'> 
<table border="0" width="100%"> 
<tr><td> 
<a href="http://www.google.com" id="mylink" target="_blank">Search:</a> 
<input type="text" name="keyword" size="20" id="keyword" onkeypress="return captureKeys(event);"> 
<a href="#" id='baidu_txt' onclick="submit_query('baidu_txt')">百度</a>  
<a href="#" id='google_txt' onclick="submit_query('google_txt')">Google</a>  
<a href="#" id='yodao_txt' onclick="submit_query('yodao_txt')">有道</a>  
<a href="#" id='yodao_blog' onclick="submit_query('yodao_blog')">博客</a>  
<a href="#" id='google_code' onclick="submit_query('google_code')">代码</a>  
<a href="#" id='google_scholar' onclick="submit_query('google_scholar')">论文</a>  
<a href="#" id='baidu_img' onclick="submit_query('baidu_img')">百图</a>  
<a href="#" id='yodao_img' onclick="submit_query('yodao_img')">有图</a>  
<a href="#" id='yodao_dict' onclick="submit_query('yodao_dict')">词典</a>  
<a href="#" id='iask_ditu' onclick="submit_query('iask_ditu')">地图</a>  
<a href="#" id='verycd' onclick="submit_query('verycd')">CD</a>  
<a href="#" id='yahoo_txt' onclick="submit_query('yahoo_txt')">Yahoo</a>  
<a href="#" id='trans_en' onclick="submit_query('trans_en')">翻译</a>  
</td></tr> 
</table> 
</body> 
</html>
Javascript 相关文章推荐
jQuery Div中加载其他页面的实现代码
Feb 27 Javascript
Colortip基于jquery的信息提示框插件在IE6下面的显示问题修正方法
Dec 06 Javascript
jQuery UI Datepicker length为空或不是对象错误的解决方法
Dec 19 Javascript
getComputedStyle与currentStyle获取样式(style/class)
Mar 19 Javascript
Jquery右下角抖动、浮动 实例代码(兼容ie6、FF)
Aug 15 Javascript
JS轮播图实现简单代码
Feb 19 Javascript
AngularJS动态添加数据并删除的实例
Feb 27 Javascript
解决低版本的浏览器不支持es6的import问题
Mar 09 Javascript
JS求Number类型数组中最大元素方法
Apr 08 Javascript
element-ui 的el-button组件中添加自定义颜色和图标的实现方法
Oct 26 Javascript
基于jquery实现的tab选项卡功能示例【附源码下载】
Jun 10 jQuery
聊聊Vue中provide/inject的应用详解
Nov 10 Javascript
让firefox支持IE的一些方法的javascript扩展函数代码
Jan 02 #Javascript
javascript getElementsByClassName 和js取地址栏参数
Jan 02 #Javascript
firefox插件Firebug的使用教程
Jan 02 #Javascript
firefox firebug中文入门教程 脚本之家新年特别版
Jan 02 #Javascript
Firefox+FireBug使JQuery的学习更加轻松愉快
Jan 01 #Javascript
20个非常棒的Jquery实用工具 国外文章
Jan 01 #Javascript
JavaScript与DropDownList 区别分析
Jan 01 #Javascript
You might like
一个漂亮的php验证码类(分享)
2013/08/06 PHP
PHP扩展程序实现守护进程
2015/04/16 PHP
php命令行写shell实例详解
2018/07/19 PHP
关于文本限制字数的js代码
2007/04/02 Javascript
javascript iframe内的函数调用实现方法
2009/07/19 Javascript
jquery跟js初始化加载的多种方法及区别介绍
2014/04/02 Javascript
jQuery中get和post方法传值测试及注意事项
2014/08/08 Javascript
jquery实现动态画圆
2014/12/04 Javascript
vue2.0s中eventBus实现兄弟组件通信的示例代码
2017/10/25 Javascript
zTree节点文字过多的处理方法
2017/11/24 Javascript
浅谈使用mpvue开发小程序需要注意和了解的知识点
2018/05/23 Javascript
Vue项目部署在Spring Boot出现页面空白问题的解决方案
2018/11/26 Javascript
浅谈vue.use()方法从源码到使用
2019/05/12 Javascript
vue视频播放插件vue-video-player的具体使用方法
2019/11/08 Javascript
超详细小程序定位地图模块全系列开发教学
2020/11/24 Javascript
[05:42]DOTA2英雄梦之声_第10期_蝙蝠骑士
2014/06/21 DOTA
[05:14]辉夜杯主赛事第二日 RECAP精彩回顾
2015/12/27 DOTA
使用python检测手机QQ在线状态的脚本代码
2013/02/10 Python
Ruby使用eventmachine为HTTP服务器添加文件下载功能
2016/04/20 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
2017/01/28 Python
用 Python 爬了爬自己的微信朋友(实例讲解)
2017/08/25 Python
python使用参数对嵌套字典进行取值的方法
2019/04/26 Python
解决Pycharm的项目目录突然消失的问题
2020/01/20 Python
Python通过kerberos安全认证操作kafka方式
2020/06/06 Python
迪卡侬印尼体育用品商店:Decathlon印尼
2020/03/11 全球购物
UML设计模式笔试题
2014/06/07 面试题
自我评价怎么写好呢?
2013/12/05 职场文书
行政人事专员岗位职责
2014/03/05 职场文书
机关出纳岗位职责
2014/04/03 职场文书
对党的十八届四中全会的期盼
2014/10/17 职场文书
财产分割协议书范本
2014/11/03 职场文书
毕业典礼邀请函
2015/01/31 职场文书
合理化建议书范文
2015/09/14 职场文书
学生会自荐信
2019/05/16 职场文书
Java存储没有重复元素的数组
2022/04/29 Java/Android
SQLServer常见数学函数梳理总结
2022/08/05 MySQL