2015-11-26 39 views
0

如果我用這個,如果條件URL參數和具體條件

(window.location.href.split("/")[6] !== "Product-A" || window.location.href.split("/")[6] !== "Product-B" || window.location.href.split("/")[6] !== "Product-C" || window.location.href.split("/")[6] !== "Intro" || window.location.href.split("/")[6] !== "vol-A-Free" || window.location.href.split("/")[6] !== "vol-B") 

和我走的結果true

但是,如果我只用這

我有這個網址

http://www.example.com/ln-en/Category/Product/Product-A/Details 

window.location.href.split("/")[6] !== "Product-A" 

我拿回false與我期待給第一個條件。什麼可能是錯的?

回答

1

window.location.href.split("/")[6]"Product-A"

因此:

window.location.href.split("/")[6] !== "Product-A"false

window.location.href.split("/")[6] !== "Product-B"true

window.location.href.split("/")[6] !== "Product-"A" || window.location.href.split("/")[6] !== "Product-B" 

相同

false || true 

這就是true

您可能想在測試中使用&&條件。

+0

我想要做的大條件是,如果window.location.href.split( 「/」)[6]如果不等於所有的值我去if語句 – therin

+0

@therin - 是的,看到答案中的最後一句話。 – Quentin

1

該表達式總是會返回true,因爲總是有一個|(或)標準是真的。你可以使用一個數組indexOf代替:

var products =["Product-A", "Product-B", "Product-C", "Intro", "vol-A-Free", "Vol-B"]; 

console.log(products.indexOf(window.location.href.split("/")[6])); 

console.log(products.indexOf(window.location.href.split("/")[6])>-1);