Home  >  Q&A  >  body text

javascript - 如何去除设置 location.hash 后的 # 号?

设置了 window.location.hash=“abc”后,url后面自动加上了“#abc”。这时候要怎么设置才能把“#abc”去掉呢?设置hash=“”只能去掉“abc”,还会残留一个#

PHPzPHPz2719 days ago772

reply all(3)I'll reply

  • 阿神

    阿神2017-04-10 14:59:53

    history api(只支持现代浏览器)

    history.replaceState(null,'',location.pathname+location.search);
    

    location.replace(导致页面重新加载)

    location.replace(location.pathname+location.search);
    

    reply
    0
  • 阿神

    阿神2017-04-10 14:59:53

    location.hash 本来就是指 URL 里 # 符号及其后的部分,如果你是要做路由映射并且不想用基于 hash 的方案,那你应该考虑 HTML5 的 History API。不过这个是要考虑向后兼容的,部署上也会要做一些配置才能正常工作。

    reply
    0
  • ringa_lee

    ringa_lee2017-04-10 14:59:53

    delete location.hash

    reply
    0
  • Cancelreply