2011-05-06 21 views
1

我有一個ajax web應用程序(JSF 2.0),它使用哈希導航。IE6/7後退/前進按鈕不改變window.location.hash

我已經在this answersetInterval()的幫助下使用了事件觸發來檢查舊版瀏覽器(主要是IE6 + 7)的值更改。

的JavaScript代碼,這是否:

window.onload = window.onhashchange = function() { 
    lastHash = window.location.hash; // To save a refresh on browsers that don't need it. 
    var fragment = document.getElementById('fragment'); 
    fragment.value = window.location.hash; 
    fragment.onchange(); 
} 

// Old Browsers That don't support onhashchange... 
var lastHash = window.location.hash; 
function checkFragment() { 
    if (window.location.hash != lastHash) { 
     lastHash = window.location.hash; 
     var fragment = document.getElementById('fragment'); 
     fragment.value = window.location.hash; 
     fragment.onchange(); 
    } 
} 

這工作得很好。意思是,當我改變散列的值時,AJAX應用程序獲取通知和更新。 這不起作用的一個實例是IE 6/7。當我按下「後退/前進」按鈕時,我可以看到url欄使用正確的哈希值更新,但windows.location.hash似乎沒有更改,所以我的SetEvent()函數未檢測到更改。

任何人都找到了解決方案? 謝謝!

+1

嗯......看起來像我這樣的JavaScript,而不是Java – 2011-05-06 13:15:00

回答

2

考慮使用jQuery Hashchange plugin以避免IE6/7兼容性問題。你可以找到code example here。對於您的情況,可以對其進行如下更改。

<script src="jquery.js"></script> 
<script src="jquery-hashchange.js"></script> 
<script> 
    $(function(){ 
     $(window).hashchange(function(){ 
      $('#fragment').val(location.hash).change(); 
     }); 

     $(window).hashchange(); 
    }); 
</script>