您的位置:首页 > Web前端 > JavaScript

用js实现判断当前网址的来路如果不是指定的来路就跳转到指定页面

2011-05-02 00:00 756 查看
<script type="text/javascript"> 
if(self!=top){top.location=self.location;} 
var ref=document.referrer; 
var domains=new Array("jb51.net/","jb51.cn/","jb51.com.cn/"); 
var refpass=false; 
for(i=0;i<=domains.length;i++){if(ref.indexOf(domains[i])>0){refpass=true;break;}} 
if(ref==""){refpass=true} 
if(!refpass){window.location.href='http://www.jb51.net';} 
</script>

推荐

<script>
function isMatch(str1,str2) 
{  
var index = str1.indexOf(str2); 
if(index==-1) return false; 
return true; 
} 
alert(window.location.hostname);
if (isMatch(window.location.hostname,'www.jb51.net') == false){window.location.href="http://www.jb51.net";}
</script>


下面我们一个网站开发中用到的

function gotourl()
{
 var url = "http://www.jb51.net"; 
 var localurl = document.url; 
 if( localurl.substring(0,url.length) != url )
 {
  location.href=url;
 }
}
gotourl();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: