2012-11-18 26 views
1

有什麼不對的語法?這似乎並不爲我工作,即使如果我提醒哈希標籤和它與我的if語句匹配。如果(window.location.hash!= =「#搜尋」)

我的代碼:

if(window.location.hash) { 
    var thehash = window.location.hash; 
    if (thehash !== "#search"){ 

     alert(thehash); // returns "#search" 

     thehash = thehash.replace(/#/g, '/'); 
     window.location.replace("http://url.com/" + thehash + "/"); 
    } 
} 

爲什麼會出現這種重定向仍然含有#搜尋

+0

無法網址複製。您的代碼不會導致重定向URL時,片段是'#search'。在Chrome v23中測試過。 – lanzz

+1

Futhermore,'thehash'沒有定義。也許這是由於複製的摘錄。 –

+0

@DavidMüller感謝,現在修復它,對不起 –

回答

4

您需要return falsealert之後,所以它不會運行函數的其餘部分

+0

謝謝,keune..I've更新我的代碼和它仍然不起作用。我在做你說錯了嗎?感謝 –

+0

沒關係,你定了!謝謝! –