IE6 / 7后退/前进按钮不改变window.location.hash [英] IE6/7 Back/Forward button don't change window.location.hash

查看:113
本文介绍了IE6 / 7后退/前进按钮不改变window.location.hash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用哈希进行导航的AJAX Web应用程序(JSF 2.0)。

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

我用两个事件的<帮助射击href="http://stackoverflow.com/questions/3475076/retrieve-the-fragment-hash-from-a-url-and-inject-the-values-into-the-bean/3477285#3477285">this回答和的setInterval()来为您在旧的浏览器(主要是IE6 + 7)。价值变动

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

JavaScript的code,做这样的:

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

这工作得很好。意思是,当我改变了AJAX应用程序得到通知,并更新哈希值。 在这不工作的一个例子是IE 6/7。当我preSS后退/前进按钮,我可以看到地址栏被用正确的散列值更新,但 windows.location.hash 好像没有不改变,所以我的 SetEvent的()功能不检测更改。

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!

推荐答案

考虑使用的jQuery插件Hashchange ,以避免IE6 / 7的兼容性头痛。你可以找到 code例如这里。它可以进行如下的情况下进行更改。

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>

这篇关于IE6 / 7后退/前进按钮不改变window.location.hash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆