【问题标题】:IE6/7 Back/Forward button don't change window.location.hashIE6/7 后退/前进按钮不改变 window.location.hash
【发布时间】:2011-08-20 04:38:13
【问题描述】:

我有一个使用哈希进行导航的 ajax webapp (JSF 2.0)。

我在 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() 函数没有检测到变化。

有人找到解决办法了吗? 谢谢!

【问题讨论】:

  • 嗯...在我看来像 javascript,而不是 java

标签: javascript ajax jsf-2


【解决方案1】:

考虑使用 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>

【讨论】:

    猜你喜欢
    • 2019-07-17
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2016-11-07
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多