2012-02-19 81 views
1

我遇到了Firefox中hashchange事件的問題。我們使用Ben Alman提供的JQuery hashchange插件。代碼如下。firefox中的jquery hashchange問​​題

$(window).hashchange(function (e) { 
    alert("Hello"); 
    //we want to perform a post in here. 
}); 
var temp = "#123"; 
if (temp !== "") { 
    if (window.location.hash == temp) { 
     $(window).hashchange(); 
    } 
    else{ 
     window.location.hash = temp; 
    } 
} 
else { 
    window.location.hash = "#Home/Home"; 
}; 

現在能正常工作在IE9和Chrome,但在Firefox中,我看到了警報,但只要我點擊確定,頁面刷新,再次顯示警告,並繼續無限。火狐瀏覽器使用了一些我不知道的奇怪行爲嗎?還是隻有一些其他隱藏得更深的問題?

+0

我沒有這個問題在FF 9.0.1或10.0.2。它顯示警報,我點擊確定,然後什麼都沒有。 – glortho 2012-02-19 22:13:48

+0

請將您的解決方案添加爲答案,以便將來人們可以輕鬆找到答案。 – Stedy 2012-02-20 01:22:33

+0

會做什麼,只是等待8小時的時間框架通過 – 2012-02-20 05:02:16

回答

0

在某些瀏覽器中,window.location.hash包含#,在某些情況下,如果在比較代碼中的哈希值時忽略它,那麼效果會更好。

試試這個。

$(window).hashchange(function (e) { 
    alert("Hello"); 
    //we want to perform a post in here. 
}); 
//Remove hash from here which will be compared with window.location.hash 
var temp = "123"; 
if (temp !== "") { 
    //Replace # by empty nothing 
    if (window.location.hash.replace('#', '') == temp) { 
     $(window).hashchange(); 
    } 
    else{ 
     window.location.hash = '#' + temp;//Now add the hash here 
    } 
} 
else { 
    window.location.hash = "#Home/Home"; 
}; 
+0

感謝您的提示,但這不是問題。我們發現問題的解決方案在這裏 http://stackoverflow.com/questions/7110023/firefox-6-infinite-page-refresh-with-page-with-hash-tags – 2012-02-19 22:22:09