Javascript if语句中的多个链接

Javascript if语句中的多个链接,javascript,if-statement,hyperlink,Javascript,If Statement,Hyperlink,我有一个脚本,它详细说明了某个元素在特定页面上不显示。下面是我使用的代码: <script type="text/javascript"> function callOnPageLoad() { var url = window.location.href; if(url == "http://www.exampledomain.com") { document.getElementById('rolex').style.display =

我有一个脚本,它详细说明了某个元素在特定页面上不显示。下面是我使用的代码:

   <script type="text/javascript">
   function callOnPageLoad()
   {
   var url = window.location.href;
   if(url == "http://www.exampledomain.com")
   {
   document.getElementById('rolex').style.display = 'none';
   }
   }
   </script>     

函数callOnPageLoad()
{
var url=window.location.href;
如果(url==”http://www.exampledomain.com")
{
document.getElementById('rolex').style.display='none';
}
}
但是我需要在if语句中添加一些url,正确的方法是什么


非常感谢

我准备生成关联数组,然后检查字符串是否已设置。它可以从AJAX、不同的脚本等中获得,并且不会被编码到if中

if(url == "http://www.exampledomain.com" || url == "http://www.anotherdomain.com")
{
}
   <script type="text/javascript">
   var urlArray = { "http://www.exampledomain.com" : true, "http://www.exampledomain.com/foobar.html" : true };
   function callOnPageLoad()
   {
   var url = window.location.href;
   if( urlArray[url] )
   {
   document.getElementById('rolex').style.display = 'none';
   }
   }
   </script>     

var urlArray={”http://www.exampledomain.com“:对,”http://www.exampledomain.com/foobar.html“:true};
函数callOnPageLoad()
{
var url=window.location.href;
if(urlArray[url])
{
document.getElementById('rolex').style.display='none';
}
}

在if块中添加更多条件:

   <script type="text/javascript">
   function callOnPageLoad()
   {
   var url = window.location.href;
   if(url == "http://www.exampledomain.com" || url == "anotherurl" || url == "andanother")
   {
   document.getElementById('rolex').style.display = 'none';
   }
   }
   </script>

函数callOnPageLoad()
{
var url=window.location.href;
如果(url==”http://www.exampledomain.com“| | url==”另一个url“| | url==”另一个“)
{
document.getElementById('rolex').style.display='none';
}
}

拥有一个URL数组并进行迭代

function callOnPageLoad()
{
   var urls = [
      "http://www.exampledomain.com",
      "http://www.exampledomain2.com"
   ];
   var url = window.location.href;
   for ( var i=0; i < urls.length; i++ ) {
      if(url == urls[i])
      {
          document.getElementById('rolex').style.display = 'none';
          break;
      }
   }   

}
函数callOnPageLoad()
{
变量URL=[
"http://www.exampledomain.com",
"http://www.exampledomain2.com"
];
var url=window.location.href;
对于(var i=0;i
您可以创建这些URL的数组并运行它们,这将是解决问题的更动态的方法

使用长if语句是不可取的,因为您可能会在此处丢失一个字符或在此处丢失一点逻辑


如果您的url指向外部url或与其他模式相匹配,您可以将它们与其他url区分开来,那么您可以在不使用数组的情况下使用它。

If(url==”http://www.exampledomain.com“|| url=”http://www.url2.com“|| url=”http://www.url3.com)等等

这个代码有很多潜在的问题,Hugh,尝试在地址栏中使用一些,删除www,禁用JS等等。请注意,默认情况下,对象有一组属性,因此也会接受像
toString
eval
这样的“URL”。为了防止使用
if(urlArray.hasOwnProperty(url))
而不是
if(urlArray[url])
+1进行此操作,我认为这可能比其他建议(多个OR,运算符中,for循环)更快。当然,性能上的差异并没有什么问题,但是…我认为使用“in”操作符比使用loop更好。你有一些“in”比for循环快的基准测试吗?给我看看它在哪里工作?这并不是:
 function callOnPageLoad(type)

    {
        var url = window.location.href;
             var urls=new array("http://www.exampledomain1com","http://www.exampledomain2.com");    


        if(url in urls){
            document.getElementById('rolex').style.display = 'none';
        }
    }