JS判断网站访问来路并跳转代码

呵呵,今天一位玩淘宝客的朋友问到了我这个问题,说为什么大百度中搜索到一个站点,点击之后,就跳到了别的网站,如果在地址栏中直接输入他的网站地址就不跳转,这个问题很简单,就是通过判断来路然后如果符合要求,就跳转到指定的网站,代码如下:

1
2
3
4
5
6
7
8
<span style="color: #339933"><</span>script type<span style="color: #339933">=</span><span style="color: #3366CC">"text/javascript"</span><span style="color: #339933">></span> 
<span style="color: #003366;font-weight: bold">var</span> lailu<span style="color: #339933">=</span>document.<span style="color: #660066">referrer</span><span style="color: #339933">;</span>
reg<span style="color: #339933">=</span><span style="color: #3366CC">"http://www.baidu.com/"</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//来路的URL主机地址</span>
<span style="color: #003366;font-weight: bold">var</span> myreg<span style="color: #339933">=</span><span style="color: #003366;font-weight: bold">new</span> RegExp<span style="color: #009900">(</span>reg<span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000066;font-weight: bold">if</span> <span style="color: #009900">(</span>myreg.<span style="color: #660066">test</span><span style="color: #009900">(</span>lailu<span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
	location.<span style="color: #660066">href</span><span style="color: #339933">=</span><span style="color: #3366CC">"http://www.chhua.com"</span><span style="color: #339933">;</span><span style="color: #006600;font-style: italic">//要跳到的站点</span>
	<span style="color: #009900">}</span>
<span style="color: #339933"></</span>script<span style="color: #339933">></span>

其实,这是SEO的作弊手法,通过ASP和PHP均可以实现,不过,我还是建议用JS,因为,搜索引擎的爬虫不会执行JS代码,如果是ASP和PHP则会执行。