【问题标题】:Jquery address: unable to handle incoming deeplinks?Jquery 地址:无法处理传入的深层链接?
【发布时间】:2010-12-06 10:49:21
【问题描述】:

我在我的页面上使用这个基于 AJAX 加载的插件,老实说,它可以很好地处理所有事情,除非我尝试捕获其中没有哈希片段的传入链接。

地址栏
传入链接-> hostname.com/path/
用户导航到另一个 ajax 状态-> hostname.com /path/ #hash
在这里,我希望插件不要将路径包含在地址栏中,因为哈希代表了这一点。

AJAX GET
传入链接-> hostname.com/path/
用户导航到另一个 ajax 状态-> hostname.com/path/
实际请求确实加载了正确的状态。

有没有办法让它不添加地址栏的路径?
例如
主机名/#web-development
而不是:
主机名/web-development/#web-development

点击处理

$('a.internalLink').live('click', function(event) {
event.preventDefault(); clickedLink = $(this); $.address.value(clickedLink.attr('href').replace(base,'')); });

【问题讨论】:

    标签: jquery ajax jquery-address


    【解决方案1】:

    您应该捕获锚标记被点击的事件,并使用event.preventDefault();防止发生默认操作

    $(document).delegate('a', 'click', function (event) {
        var targetHref = this.href;
    
        window.location.href = targetHref; // or whatever
    
        event.preventDefault(); // stop the page changing.
    });
    

    您可能还对新增的 HTML 5 感兴趣,这些新增内容允许操纵浏览器历史记录;即您可以使用 AJAX 更新页面,但仍然更改地址。请参阅here 了解更多详情(Mozilla 开发者中心)。

    【讨论】:

    • 您好,感谢您的帮助,我已经准备好这段代码:
      $('a.internalLink').live('click', function(event) { event. preventDefault();
      clickedLink = $(this);
      $.address.value(clickedLink.attr('href').replace(base,''));
      })
    • 我用防止默认事件的方法更新了我的问题。
    【解决方案2】:

    我通过将任何未散列的 URLS 重定向到散列等效项来规避该问题:

    if(!window.location.hash){
      window.location.href = 'http://'+window.location.host+'/#'+window.location.pathname+window.location.search;
    };
    

    它不是那么优雅,也许有更好的解决方案,但它确实有效。

    【讨论】:

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