Javascript 从 url 中删除哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4508574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove hash from url
提问by iamserious
I am ajax-ifying the pagination in one of me projects and since I want the users to be able to bookmarks the current page, I am appending the page number via hash, say:
我正在对我的一个项目中的分页进行 ajax 化,并且由于我希望用户能够为当前页面添加书签,因此我通过哈希附加页码,例如:
onclick="callPage(2); window.location.hash='p=2'; return false;"
and thats on the hyperlink
it works fine and everything, except, when the page number is 1, i dont want to URL
to be /products#p=1
, I just want it to be /products
就是这样,hyperlink
它工作正常,一切正常,除了当页码为 1 时,我不想URL
成为/products#p=1
,我只想成为/products
I tried these variations:
我尝试了这些变化:
window.location.hash=''
works but the url is now like/products#
and I dont quite the hash there.- not using window.location.hash at all, but when the user comes back to page 1 from, say page 3, he is in page one, but url is still
/products#p=3
since I am not messing with the hash. - Google search on this led me to several minutes (about 15) of silly forums where the question was asked right, but answers were suggesting that the page jumps because the thread creator had a hash in href like
<a href="#">
and he should usejavascript:void(0)
instead. (had they never heard of Ajax?)
window.location.hash=''
有效,但 url 现在是这样的/products#
,我不太清楚那里的哈希值。- 根本不使用 window.location.hash,但是当用户从第 1 页返回到第 1 页时,比如说第 3 页,他在第 1 页,但 url 仍然是
/products#p=3
因为我没有弄乱哈希。 - 对此的 Google 搜索使我进入了几分钟(大约 15 分钟)的愚蠢论坛,在那里问题被问到了正确的问题,但答案表明页面跳转是因为线程创建者在 href 中具有哈希值,
<a href="#">
而他应该使用它javascript:void(0)
来代替。(他们从未听说过阿贾克斯吗?)
So finally, I decided to make this thread, I found several similar threads here, but all the answers ls very similar to my second point.
所以最后,我决定创建这个线程,我在这里找到了几个类似的线程,但所有的答案都与我的第二点非常相似。
so my big question still remains a question: How to kick the hash out of the URL and possibly out of the universe? (only for the first page!)
所以我的大问题仍然是一个问题:如何将哈希值踢出 URL 并可能踢出宇宙?(仅限第一页!)
回答by Homero Barbosa
history.pushState("", document.title, window.location.pathname);
回答by wombleton
Updated Answer:
更新答案:
Best way to achieve this is to follow Homero Barbosa's answer below:
实现这一目标的最佳方法是遵循以下Homero Barbosa的回答:
history.pushState("", document.title, window.location.pathname);
... or, if you want to maintain the search parameters:
...或者,如果您想维护搜索参数:
history.pushState("", document.title, window.location.pathname + window.location.search);
Original Answer, do not use this, badwrongfun:
原始答案,不要使用这个,badwrongfun:
var loc = window.location.href,
index = loc.indexOf('#');
if (index > 0) {
window.location = loc.substring(0, index);
}
... but that refreshes the page for you which seems a trifle rude after just arriving there. Grin and bear it seems to be the best option.
...但这会为您刷新页面,这在刚到达那里后似乎有点粗鲁。咧嘴一笑似乎是最好的选择。
回答by Devang Bhagdev
var urlWithoutHash = document.location.href.replace(location.hash , "" );
回答by Ronak Bokaria
Worked For me Perfectly
完美地为我工作
$(window).on('hashchange', function(e){
window.history.pushState("", document.title, window.location.pathname);
// do something...
});
回答by Mudassar ali
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;
}
}