【问题标题】:Remove hash from url从 url 中删除哈希
【发布时间】:2011-05-29 08:35:35
【问题描述】:

我在我的一个项目中对分页进行了 ajax 化处理,因为我希望用户能够为当前页面添加书签,所以我通过哈希附加页码,例如:

onclick="callPage(2); window.location.hash='p=2'; return false;"

这就是hyperlink 它工作正常,一切正常,除了当页码为 1 时,我不想将 URL 变成 /products#p=1,我只想让它成为 /products

我尝试了这些变化:

  1. window.location.hash='' 有效,但网址现在类似于 /products#,而且我不太喜欢那里的哈希值。
  2. 根本不使用window.location.hash,但是当用户从第3页回到第1页时,他在第1页,但url仍然是/products#p=3,因为我没有弄乱哈希。
  3. 对此的 Google 搜索让我进入了几分钟(大约 15 分钟)的愚蠢论坛,在这些论坛中问题被正确提出,但答案表明页面跳转,因为线程创建者在 href 中有一个像 <a href="#"> 这样的哈希值,他应该请改用javascript:void(0)。 (他们从未听说过 Ajax 吗?)

所以最后,我决定做这个帖子,我在这里找到了几个类似的帖子,但所有的答案都与我的第二点非常相似。

所以我的大问题仍然是一个问题:如何将散列踢出 URL 并可能踢出宇宙? (仅限第一页!)

【问题讨论】:

  • 我看到反引号在有序列表中无效。
  • @iamserious:如果您使用 Markdown 而不是 HTML 来创建这些列表,它们就是 ;)
  • @Kling,啊哈!这很好,谢谢!
  • @maksymko,我想这只是个人喜好,但我想要一个干净的 url,我的意思是,如果哈希之后没有任何内容,为什么还要首先添加它?另外,我想知道搜索机器人是否将 /products/products# 视为两个不同的页面?

标签: javascript ajax fragment-identifier


【解决方案1】:
history.pushState("", document.title, window.location.pathname);

【讨论】:

  • 只是快速尝试一下,它也在删除查询参数......不可取!
  • 谢谢,正是我所需要的(虽然同意,但我不介意删除查询参数(但是,可以通过简单地添加查询字符串来保持没有问题,例如history.pushState('', document.title, window.location.pathname+window.location.search);))
  • 适用于新浏览器,但 IE9 及更少版本不支持。只是想指出这一点。
  • 这也会在历史记录中创建一个条目(显然)。因此,当用户单击后退按钮时,她将在同一页面上。
  • history.replaceState( ... ) 如果您担心@enyo 问题,可能会更合适
【解决方案2】:

更新答案

实现这一目标的最佳方法是关注Homero Barbosaanswer below

history.pushState("", document.title, window.location.pathname);

...或者,如果您想维护搜索参数:

history.pushState("", document.title, window.location.pathname + window.location.search);

原来的答案,不要用这个,badwrongfun

var loc = window.location.href,
    index = loc.indexOf('#');

if (index > 0) {
  window.location = loc.substring(0, index);
}

...但这会为您刷新页面,这在刚到那里后似乎有点粗鲁。咧嘴一笑似乎是最好的选择。

【讨论】:

  • 你也可以妥协并接受像/products#First这样比/products#p=1更优雅的东西:)
  • 这对我有用,您也可以再次保存字符串而不是更改页面: var hashIndex = userUrl.indexOf('#'); if (hashIndex > 0) { userUrl = userUrl.substring(0, hashIndex); }
  • 下面是更好的答案!!
  • history.pushState("", document.title, window.location.pathname);还将删除 php 查询字符串,即“?key=hello”。我们如何在不删除查询字符串的情况下只删除哈希?
  • 我得到未定义的名称“历史”
【解决方案3】:

非常适合我

$(window).on('hashchange', function(e){
  window.history.pushState("", document.title, window.location.pathname);  
 // do something...
});

【讨论】:

  • 迄今为止最好和最简单的答案。
【解决方案4】:
var urlWithoutHash = document.location.href.replace(location.hash , "" );

【讨论】:

  • 这有问题,比如哈希是一个短词。
【解决方案5】:
function removeHash () { 
    var scrollV, scrollH, loc = window.location;
    if ("pushState" in history)
        history.pushState("", document.title, loc.pathname + loc.search);
    else {
        // Prevent scrolling by storing the page's current scroll offset
        scrollV = document.body.scrollTop;
        scrollH = document.body.scrollLeft;

        loc.hash = "";

        // Restore the scroll offset, should be flicker free
        document.body.scrollTop = scrollV;
        document.body.scrollLeft = scrollH;
    }
}

【讨论】:

  • 请在您的答案中添加解释。
  • 这是从this answer无耻复制过来的。
猜你喜欢
  • 2012-04-19
  • 2013-05-15
  • 2013-03-02
  • 1970-01-01
  • 2017-05-31
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多