我有一个使用哈希导航的ajax webapp(JSF 2.0)。
我已经在this answer和setInterval()
的帮助下使用了事件触发功能来检查旧版浏览器(主要是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是其中一个无效的实例。当我按Back / Forward按钮时,我可以看到URL栏已使用正确的哈希值更新,但是
windows.location.hash
似乎没有变化,因此我的SetEvent()
函数没有检测到变化。有人找到解决方案了吗?
谢谢!
最佳答案
考虑使用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>