为什么 jQuery Asual Address 插件会触发更改事件两次?

发布于 2024-10-19 06:09:13 字数 788 浏览 2 评论 0原文

您好,

我正在使用 jQuery Asual Address 插件: http://www.asual.com/jquery/地址/

由于某种原因,在 Firefox 和 IE(但不是 Chrome)中,当参数之一是 url(以“http://”开头)时,地址更改事件会触发两次。

这是一个小提琴示例: http://jsfiddle.net/5L6Ur/

单击“foo”链接展示了我的问题。非常感谢任何帮助。

代码:

$(function() {
    $('a').click(function(e) {
        e.preventDefault();
        $.address.value($(this).attr('href'));
    });
    var changecount = 0;
    $.address.change(function(event) {
        $('span').html(changecount++);
    });
});

<a href="?u=http://foo.bar">foo</a><br />
<a href="?u=foo.bar">bar</a><br />
<span></span>

Greetings,

I'm using the jQuery Asual Address plugin: http://www.asual.com/jquery/address/

For some reason, in Firefox and IE (but not Chrome), the address change event is firing twice when one of the parameters is a url (starts with "http://").

Here's a fiddle with an example: http://jsfiddle.net/5L6Ur/

Clicking on the "foo" link demonstrates my problem. Any help is greatly appreciated.

Code:

$(function() {
    $('a').click(function(e) {
        e.preventDefault();
        $.address.value($(this).attr('href'));
    });
    var changecount = 0;
    $.address.change(function(event) {
        $('span').html(changecount++);
    });
});

<a href="?u=http://foo.bar">foo</a><br />
<a href="?u=foo.bar">bar</a><br />
<span></span>

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

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

发布评论

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

评论(5

雨后咖啡店 2024-10-26 06:09:13

尝试使用internalChange 和externalChange 函数。

http://jsfiddle.net/5L6Ur/9/

Try using the internalChange and externalChange functions.

http://jsfiddle.net/5L6Ur/9/

牵你的手,一向走下去 2024-10-26 06:09:13

我解决此问题的方法是设置 searching 变量。

像这样:

$(function () {
  var searching = false;
  $('form.ajaxified').live('ajax:beforeSend.rails', function(){
    $.address.value('?' + $('form.ajaxified').serialize());
    searching = true;
  });
  $('form.ajaxified').live('ajax:complete.rails', function(){
    searching = false;
  });
  $.address.externalChange(function(event) {
    if (!searching) {
      $('form.ajaxified').submit();
    }
  });
});

当我作为用户触发它(通过提交表单)时,这可以防止双重ajax请求,但当我点击后退按钮或重新加载页面时运行它。希望有帮助。

PS 这些事件是特定于 Rails 的,但可以使用 JQuery Ajax 回调来完成相同的操作。

My workaround for this the problem is setting the searching variable.

something like:

$(function () {
  var searching = false;
  $('form.ajaxified').live('ajax:beforeSend.rails', function(){
    $.address.value('?' + $('form.ajaxified').serialize());
    searching = true;
  });
  $('form.ajaxified').live('ajax:complete.rails', function(){
    searching = false;
  });
  $.address.externalChange(function(event) {
    if (!searching) {
      $('form.ajaxified').submit();
    }
  });
});

This prevents the double ajax requests when I trigger it as a user (by submitting a form) but runs it when I hit back button or reload the page. Hope that helps.

PS The events are rails specific, but the same could be accomplished using JQuery Ajax calbacks.

揽清风入怀 2024-10-26 06:09:13

我花了很多时间来解决同样的问题。我已经缩小了原因,但除了回避之外我没有其他解决方案。该问题是由您在示例中创建的哈希值中的“=”引起的。我猜测这个问题与插件执行的解析有关,因为我用许多常见的分隔符(“=”、“&”、“|”、“:”、“;”重新创建了问题) )。您可以通过将这些麻烦的字符替换为其他字符(我使用“/”和“.”)来避免双重触发问题。

这是我所看到的症状的更具体解释。 IE 和 Firefox 中都会使用 .change() 方法或 .externalChange() 和 .internalChange 方法的组合来双重触发更改事件。如果使用 .change() 方法,则不会真正指示发生了什么,但使用内部和外部方法可以让您更深入地了解问题。内部更改事件似乎表现正常,仅在触发事件实际上是内部时触发。然而,外部事件似乎一直在触发,无论触发事件是内部的还是外部的。结果是外部事件“正确”触发,这意味着仅触发 externalChange() 方法。内部事件“不正确地”触发,导致双重触发——internalChange() 方法按预期触发,但 externalChange() 方法也会触发。

希望这有帮助。

I spent a lot of time troubleshooting this same issue. I've narrowed in on the cause, but I don't have a solution to offer, other than avoidance. The problem is caused by the "=" in the hash value you are creating in your example. I'm guessing the issue has to do with the parsing performed by the plugin, because I've recreated the problem with many common delimiter characters ("=", "&", "|", ":", ";"). You can avoid the double-firing problem by replacing these troublesome characters with others (I'm using "/" and ".").

Here's a more specific explanation of the symptoms I'm seeing. The double-firing of the change event happens in both IE and Firefox using either the .change() method or a combination of the .externalChange() and .internalChange methods. If using the .change() method, there's no real indication of what's happening, but using the internal and external methods allows you to see a little deeper into the problem. The internal change event seems to behave properly, only firing when the triggering event is actually internal. However, the external event seems to fire all the time, regardless of whether the triggering event is internal or external. The result is that external events fire "correctly," meaning that only the externalChange() method is fired. Internal events fire "incorrectly," result in double-firing -- the internalChange() method fires as expected, but the externalChange() method fires as well.

Hope this helps.

别靠近我心 2024-10-26 06:09:13

看起来 github 上该插件的最新版本(1.4)修复了这个问题。

这是使用最新插件的更新示例:
http://jsfiddle.net/5L6Ur/10/

Looks like the latest version of the plugin on github (1.4) fixes this.

Here's an updated example using the latest plugin:
http://jsfiddle.net/5L6Ur/10/

音盲 2024-10-26 06:09:13

你不应该用 $(function () { }); 包装它。功能。然后就可以了

You should not wrap it with the $(function () { }); function. Then it works

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