js怎么判断域名是否是某个域名

就是js判断访问域名的主域名是不是某个域名,不然就跳转


<script>
       try
           {
               if(self.location == "http://apple.ya37.com/")
               {
                   top.location.href = "http://ya37.com/apple/";
               }
               else if(self.location == "http://girl.ya37.com/")
               {
                   top.location.href = "http://ya37.com/girl/";
               }
               else if(self.location == "http://simple.ya37.com/")
               {
                   top.location.href = "http://ya37.com/simple/";
               }
               else {document.write ("错误的访问地址")}
           }
       catch(e){ }
   </script>

看明白了吧,如果访问者输入的域名是"http://apple.ya37.com/"的话,就访问"http://ya37.com/apple/"。

用indexOf判断


if(location.toString().indexOf("abc.com") <= -1)     /*如果当前网址中没有abc.com*/ 
{ 
document.location.href="/b.htm";   /*跳转到b.htm*/ 
} 
else
{
document.location.href="/a.htm";   /*有的话,跳转到a.htm*/ }