通過JS實現來路判斷,如果不是指定來路跳轉到指定頁面。這個功能有多種實現辦法,這里我們介紹常見的幾種,分享給大家。
第一種
<script type="text/javascript"> if(self!=top){top.location=self.location;} var ref=document.referrer; var domains=new Array("vevb.com/","tao2t.com/","tao2t.net/"); 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.49028c.com';} </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.49028c.com') == false){window.location.href="http://www.49028c.com";}</script>
第三種,這是我們一個網站開發中用到的
function gotourl(){ var url = "http://www.49028c.com"; var localurl = document.url; if( localurl.substring(0,url.length) != url ) { location.href=url; }}gotourl();
以上是用js實現判斷當前網址的來路如果不是指定的來路就跳轉到指定頁面_javascript技巧的全部內容。
新聞熱點
疑難解答