javascript如果条件麻烦

时间:2016-05-10 18:40:46

标签: javascript

我试图在javascript中写这个条件:

if(window.location.pathname != '/our-communities.php' && (window.location.pathname == '/upcoming-communities.php' && window.location.search != '')){

我想说的是,如果页面不是our-communities.php,如果页面不是即将到来的 - communities.php和window.location.search不为空,则运行代码。

所以这个条件应该在除了our-communities.php之外的每个页面上运行,但是只有在window.location.search为空的情况下才可以在coming-communities.php上运行。

1 个答案:

答案 0 :(得分:1)

if (window.location.pathname != "/our-communities.php" || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {

}

有一点需要注意,如果页面为"/upcoming-communities.php",则第一个条件将始终为真,这意味着第二个OR语句无关紧要,如果您想强制执行,则需要额外的条件。

if ((window.location.pathname != "/our-communities.php" && window.location.pathname != "/upcoming-communities.php") || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {

}