从URL删除哈希 [英] Remove hash from url

查看:232
本文介绍了从URL删除哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AJAX-ifying分页在我的项目之一,因为我希望用户能够到书签当前页面,我通过追加散列页号,说:

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:

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

和对超链接它工作得很好,一切,只是,当页面数为1,我不想网​​址 /产品#P = 1 ,我只是希望它是 /产品

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

我尝试了这些变化:

  1. window.location.hash =''的作品,但网址是现在这个样子 /产品#,我不相当哈希那里。
  2. 在不使用window.location.hash可言,但是当用户回来到第1页的,说的第3页,他是在一个页面上,但网址仍然 /产品#p值= 3 ,因为我不是与哈希搞乱。
  3. 在此谷歌搜索使我的傻论坛几分钟(约15),其中有人问正确的,但答案是提示页面跳转,因为线程的创建者曾在HREF哈希像 < A HREF =#> ,他应该用的javascript:无效(0)代替。 (如果他们从未听说过阿贾克斯的?)
  1. window.location.hash='' works but the url is now like /products# and I dont quite the hash there.
  2. 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.
  3. 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 use javascript:void(0) instead. (had they never heard of Ajax?)

所以最后,我决定把这个线程,我发现了几个类似的线程在这里,但所有的答案LS非常相似,我的第二点。

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!)

推荐答案

最好的奥梅罗巴博萨的< A HREF =htt​​p://stackoverflow.com/a/9643338/22419>下面答案:

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);

编辑,老,不使用,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.

这篇关于从URL删除哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆