javascript - 如何去除设置 location.hash 后的 # 号?
PHPz
PHPz 2017-04-10 14:57:53
0
3
766

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

PHPz
PHPz

学习是最好的投资!

全部回复(3)
阿神

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

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

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

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

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

左手右手慢动作

delete location.hash

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!