【问题标题】:Jquery hashchange problems in firefoxFirefox中的Jquery hashchange问​​题
【发布时间】:2012-02-19 21:45:22
【问题描述】:

我在 Firefox 中遇到了 hashchange 事件的问题。我们使用的是 Ben Alman 提供的 JQuery hashchange 插件。代码如下。

$(window).hashchange(function (e) {
    alert("Hello");
    //we want to perform a post in here.
});
var temp = "#123";
if (temp !== "") {
    if (window.location.hash == temp) {
        $(window).hashchange();
    } 
    else{
        window.location.hash = temp;
    }
} 
else {
    window.location.hash = "#Home/Home";
};

现在这在 IE9 和 Chrome 中运行良好,但是在 Firefox 中,我看到了警报,但只要我单击确定,页面就会刷新,再次显示警报,并无限继续。 Firefox 使用了某种我不知道的奇怪行为吗?还是只是其他一些隐藏得更深的问题?

【问题讨论】:

  • 我在 FF 9.0.1 或 10.0.2 中没有这个问题。它显示警报,我点击 OK,然后什么也没有。
  • 请将您的解决方案添加为答案,以便人们在未来轻松找到它。
  • 会做,只是等待8小时的时间框架过去

标签: javascript jquery hashchange


【解决方案1】:

在某些浏览器中,window.location.hash 包含 #,而在某些浏览器中,如果您在比较代码中的哈希值时忽略它会更好。

试试这个。

$(window).hashchange(function (e) {
    alert("Hello");
    //we want to perform a post in here.
});
//Remove hash from here which will be compared with window.location.hash
var temp = "123";
if (temp !== "") {
    //Replace # by empty nothing
    if (window.location.hash.replace('#', '') == temp) {
        $(window).hashchange();
    } 
    else{
        window.location.hash = '#' + temp;//Now add the hash here
    }
} 
else {
    window.location.hash = "#Home/Home";
};

【讨论】:

【解决方案2】:

我们将问题定位在 MicrosoftAjax.js 中,并找到了以下解决方案: Firefox 6 Infinite Page Refresh With Page With Hash Tags

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多