IE6/7 后退/前进按钮不改变 window.location.hash

发布于 2024-11-05 11:31:24 字数 1154 浏览 0 评论 0原文

我有一个 ajax web 应用程序 (JSF 2.0),它使用哈希进行导航。

我在 这个答案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 是其中一个不起作用的实例。当我按后退/前进按钮时,我可以看到网址栏已更新为正确的哈希值,但 windows.location.hash 似乎没有改变,所以我的 SetEvent( ) 函数未检测到更改。

有人找到解决这个问题的方法吗? 谢谢!

I have an ajax webapp (JSF 2.0) that uses hash for navigation.

I've used both event firing with help of this answer and setInterval() to check for value change in older browsers (Mainly IE6+7).

The Javascript code that does this:

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();
    }
}

This works nicely. Meaning, when I change the value of the hash the AJAX app gets the notification and updates.
One instance in which this doesn't work is IE 6/7. When I press Back/Forward buttons, I can see the url bar gets updated with the correct hash values, but the windows.location.hash doesn't seem to change and so my SetEvent() function doesn't detect the change.

Anyone found a solution to this?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2024-11-12 11:31:24

考虑使用 jQuery Hashchange 插件 以避免 IE6/7 兼容性问题。您可以在此处找到代码示例。可以根据您的情况进行如下更改。

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

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

Consider using jQuery Hashchange plugin to avoid IE6/7 compatibility headaches. You can find code example here. It can be altered as follows for your case.

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

        $(window).hashchange();
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文