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()
js的关键字有很多啊break delete function return typeof
case do if switch var
catch else in this void
continue false instanceof throw while
debugger finally new true with
default for null try
详细出处参考:http://www.jb51.net/article/13015.htm