2014-10-02 63 views
-1

我隐藏基于特定URL的内容。使用位置==来指定多个URL

我已经做了这个声明一个特定的URL,它工作正常,但我试图声明多个;下面的尝试不起作用,即使用逗号分开。

if(location=="http://domainone.com/" "http://domaintwo.com/index.php/my-catalog/single-item-view") { 
+2

作为我的回答的一个附注,你的javascript应该对页面上的内容而不是URL起作用,你现在的方法会变得相当混乱。 – 2014-10-02 23:52:09

回答

1
if(location=="http://domainone.com/" || location == "http://domaintwo.com/index.php/my-catalog/single-item-view") { 

||符号表示“或”,这意味着整个条件将导致true如果任一条件都为真。这与&&相比,意思是“和”,意思是只有所有条件都成立时,整个条件才会成立。

2

您需要使用logical operator多个条件组合:

if (
    location == "http://domainone.com/" || 
    location == "http://domaintwo.com/index.php/my-catalog/single-item-view" 
) 

或者你可以使用可接受的URL的array并检查当前location是你whitelist数组中。

var whitelist = [ 
    "http://domainone.com/", 
    "http://domaintwo.com/index.php/my-catalog/single-item-view" 
]; 
if (whitelist.indexOf(window.location) != -1)