从URL中删除哈希

时间:2012-04-05 09:39:18

标签: jquery browser cross-browser

使用window.location.hash=''页面从URL中删除哈希后,在firefox中重新加载。

修改

示例:

wwww.Mysite.come/#page=1

点击按钮后,我将使用以下代码删除哈希值

window.location.hash=''

删除哈希页面后,在firefox中重新加载。

我不想重新加载页面我只想从URL中删除哈希

如何解决?

5 个答案:

答案 0 :(得分:21)

以防其他人仍在寻找解决方案。页面加载时尝试此操作。

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

答案 1 :(得分:4)

来自https://developer.mozilla.org/en/DOM/window.location

  

实施例

     

每当修改位置对象的属性时,都会生成文档   将使用URL加载,就像window.location.assign()一样   使用修改后的URL调用。

这个问题我想用jQuery解决你想要的问题:

Change hash without reload in jQuery

其他相关问题:

Change the URL in the browser without loading the new page using JavaScript

How to remove the hash from window.location with JavaScript without page refresh?

How can I change Firefox window.location.hash without creating a page reload?

答案 2 :(得分:2)

如果我理解正确,

<a href="#someElementID" id="myLinkName">Some Text</a>

在浏览器中点击上面的链接通常会在地址栏中添加一个哈希值,例如 www.websitename.com#someElementID &lt; - 这就是你想要的阻止,是吗?

我刚刚测试的解决方案正在运行且不刷新页面是:

event.preventDefault();

这适用于'click()'事件,该事件涉及链接到元素id的锚标记,例如上面的锚标记示例。在行动中,它会在您的“click()”事件中显示如下:

<script>
    $('#myLinkName').click(function(){
        event.preventDefault();
        //the rest of the function is the same.
    });
</script>

现在,点击相同的链接会使地址栏保留相同的网址www.websitename.com,没有点击锚点添加的哈希值。

答案 3 :(得分:2)

我们可以通过在click函数中返回false来删除/转义附加哈希值。

<script>
    $('#add_user').click(function(){       
     //your custom function code here..
    return false;
});
</script>

<a id="add_user" href="#">Add Card</a>

答案 4 :(得分:0)

您会发现w3schools中的这个示例非常有用,而且它为您提供了与所有浏览器兼容的优雅滚动动作,请查看以下代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div id="navbar">
    <a href="#home"> Home </a>
    <a href="#" id="deliveryLink"> Delivery </a>
    <a href="#" id="postLink"> Post </a>
    <a href="#" id="recepiesLink"> Recepies </a>
    <a href="#" id="contactLink"> Contact </a>
</div>
    <div id="content">
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
        <br> ////////////////////I copy paste a lot of this, i don't have content at the moment
      </div>

<script>
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "165px";
  } else {
    document.getElementById("navbar").style.top = "-50px";
  }
  prevScrollpos = currentScrollPos;
}
</script>

</body>
</html>