这里有一个简单的站内搜索功能
<SCRIPT language=javascript><!--
function go(formname)
{
var url = http://www.163ns.com
formname.method = "get"
if (formname.myselectvalue.value == "1") {
document.search_form1.word.value = document.search_form1.word.value " site:www.163ns.com"
}
formname.action = url
return true
}
//-->
</SCRIPT>
<form name="search_form1" target="_blank" onsubmit="return go(this)">
<input name=word size="30" value="请输入关键字" onMouseOver="this.focus()" onBlur="if (value ==''){value='请输入关键字'}" onFocus="this.select()" onClick="if(this.value=='请输入关键字')this.value=''">
<input type="submit" value="搜索"><br>
<INPUT name=myselectvalue type=hidden value=0>
<INPUT name=tn type=hidden value="sayyes">
<INPUT name=cl type=hidden value="3">
<INPUT CHECKED name=myselect onclick=javascript:this.form.myselectvalue.value=0 type=radio value=0>
<FONT color=#0000cc style="FONT-SIZE: 12px">互联网
<INPUT name=myselect onclick=javascript:this.form.myselectvalue.value=1 type=radio value=1>
<FONT color=#ff0000 style="FONT-SIZE: 12px">站内
</form>
一:对文章关键字的搜索(数组的方法):
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#search{
height:40pxwidth: 200pxfont-size: 30pxtext-align: center
}
#div {
font-size: 20pxwidth: 600px
}
#div span{
color: red
}
</style>
</head>
<body >
<input type="text" name="search" id="search" value="馋" onclick="this.value='' "/>
<input type="button" name="" id="bbtn" value="搜索" />
<div id="div">
<p>馋,在英文里找不到一个十分适当的字。罗马暴君尼禄,以至于英国的亨利八世,在大宴群臣的时候,常见其撕下一根根又粗又壮的鸡腿,举起来大嚼,旁若无人,好一副饕餮相!但那不是馋。埃及废王法鲁克,据说每天早餐一口气吃二十个荷包蛋,也不是馋,只是放肆,只是没有吃相。对有某一种食物有所偏好,于是大量的吃,这是贪多无厌。馋,则着重在食物的质,最需要满足的是品味。上天生人,在他嘴里安放一条舌,舌上还有无数的味蕾,教人焉得不馋?馋,基于生理的要求;也可以发展成为近于艺术的趣味。
也许我们中国人特别馋一些。馋字从食,有声。毚音谗,本义是狡兔,善于奔走,人为了口腹之欲,不惜多方奔走以膏馋吻,所谓“为了一张嘴,跑断两条腿”。</p>
</div>
<script type="text/javascript">
var bbtn=document.getElementById('bbtn')
var search=document.getElementById('search')
var text=document.getElementsByTagName('p')[0]
var text1=text.innerHTML
var arr=[]
var str=text1
bbtn.onclick=function(){
str=text1
arr=search.value
str=str.split(arr).join('<span>'+arr+'</span>')
text.innerHTML=str
}
</script>
</body>
</html>
二:对li表单的搜索:
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
padding: 0margin: 0
}
#content{
margin: 100px 300pxdisplay: block
}
input{
height: 50pxwidth: 400pxfont-size: 30pxline-height: 50px
}
ul{
}
li{
margin:10px 0pxdisplay: block
}
</style>
</head>
<body>
<div id="content">
<ul>
<input type="" name="" id="" value="" placeholder="请输入城市的名称或拼音"/>
<li pname="北京" cname="bj">北京</li>
<li pname="株洲" cname="zz">株洲</li>
<li pname="上海" cname="sh">上海</li>
<li pname="长沙" cname="cs">长沙</li>
<li pname="杭州" cname="hz">杭州</li>
<li pname="天津" cname="tj">天津</li>
<li pname="西安" cname="xa">西安</li>
<li pname="成都" cname="cd">成都</li>
<li pname="武汉" cname="wh">武汉</li>
<li pname="南京" cname="nj">南京</li>
</ul>
</div>
<script type="text/javascript">
var li=document.getElementsByTagName('li')
var text=document.getElementsByTagName('input')[0]
function search(){
var arr=text.value
for(var i=0i<li.lengthi++)
{
li[i].style.display="none"
if(li[i].getAttribute('pname').indexOf(arr)!=-1||li[i].getAttribute('cname').indexOf(arr)!=-1)
{
li[i].style.display="block"
}
}
}
text.oninput=function(){
search()
}
</script>
</body>
</html>
js获取搜索引擎搜索关键词有两种方法可以实现:
第一种方法:
var str1=""var str2= ""
var refer=document.referrer
var sosuo=refer.split(".")[1]
var grep=null
var str=null
var keyword=null
var skey="xx"
var ykey=""
switch(sosuo){
case "baidu":
grep=/wd\=.*\&/i
str=refer.match(grep)
keyword=str.toString().split("=")[1].split("&")[0]
//console.log(decodeURIComponent(keyword))
ykey=decodeURIComponent(keyword)
addCookie('key',decodeURIComponent(keyword),1)
//alert(decodeURIComponent(keyword))
break
case "google":
grep=/&q\=.*\&/i
str=refer.match(grep)
keyword=str.toString().split("&")[1].split("=")[1]
//console.log(decodeURIComponent(keyword))
ykey=decodeURIComponent(keyword)
addCookie('key',decodeURIComponent(keyword),1)
break
case "sogou":
grep=/query\=.*\&/i
str=refer.match(grep)
keyword=str.toString().split("&")[1].split("&")[2]
//console.log(decodeURIComponent(keyword))
ykey=decodeURIComponent(keyword)
addCookie('key',decodeURIComponent(keyword),1)
//alert(decodeURIComponent(keyword))
break
default:
addCookie('key','',1)
}
var ckey=(getCookie('key'))
//alert(ckey)
if(ykey.indexOf(skey)>-1)
{
}else
{
}
function deleteCookie(name){
var date=new Date()
date.setTime(date.getTime()-10000)
document.cookie=name+"=v expires="+date.toGMTString()
}
function getCookie(name){
var strCookie=document.cookie
var arrCookie=strCookie.split(" ")
for(var i=0i < arrCookie.lengthi++){
var arr=arrCookie[i].split("=")
if(arr[0]==name)return arr[1]
}
return ""
}
function addCookie(name,value,expiresHours){
var cookieString=name+"="+escape(value)
//判断是否设置过期时间
if(expiresHours>0){
var date=new Date()
date.setTime(date.getTime+expiresHours*3600*1000)
cookieString=cookieString+" expires="+date.toGMTString()
}
document.cookie=cookieString
}
document.write(ykey)
if(ykey=="")
{}
else
{
document.write("")
}
第二种方法:
function getDomainQuery(url) {var d = []
var st = url.indexOf('//', 1)
var _domain = url.substring(st + 1, url.length)
var et = _domain.indexOf('/', 1)
d.push(_domain.substring(1, et))
d.push(_domain.substring(et + 1, url.length))
return d
}
function route() {
var eg = []
eg.push(['baidu', 'wd'])
eg.push(['google', 'q'])
eg.push(['soso', 'w'])
eg.push(['yodao', 'q'])
eg.push(['bing', 'q'])
eg.push(['yahoo', 'q'])
eg.push(['sogou', 'query'])
eg.push(['gougou', 'search'])
eg.push(['360', 'q'])
var dq = getDomainQuery(document.referrer)
var keyword=null
var grep=null
var str=null
for(var el in eg){
var s = eg[el]
var DandQ=String(s).split(",") //字符分割
if (dq[0].indexOf(DandQ[0])>0){
eval("grep=/"+DandQ[1]+"\=.*\&/i")
str = dq[1].match(grep)
keyword = str.toString().split("=")[1].split("&")[0]
keyword = decodeURIComponent(keyword)
alert(keyword)
return false
}
}
}
route()