jQuery 从 URL 中删除哈希

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10026223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:44:07  来源:igfitidea点击:

Removing hash from URL

jquerybrowsercross-browser

提问by suma

After removing hash from URL using window.location.hash=''page getting reloaded in firefox.

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

EDIT

编辑

Example:

例子:

wwww.Mysite.come/#page=1

wwww.Mysite.come/#page=1

On a click of button I am removing hash value using following code

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

window.location.hash=''

window.location.hash=''

After removing the hash page is getting reloaded in firefox.

删除哈希页面后,Firefox 将重新加载。

I don't want to reload page I just want to remove hash from URL

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

How to fix it?

如何解决?

采纳答案by A B

From https://developer.mozilla.org/en/DOM/window.location:

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

Examples

Whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.

例子

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

This question I think addresses what you want using jQuery:

我认为这个问题解决了你想要使用 jQuery 的问题:

Change hash without reload in jQuery

在 jQuery 中无需重新加载即可更改哈希

Other related questions:

其他相关问题:

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

在浏览器中更改 URL 而不使用 JavaScript 加载新页面

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

如何在不刷新页面的情况下使用 JavaScript 从 window.location 中删除哈希?

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

如何在不创建页面重新加载的情况下更改 Firefox window.location.hash?

回答by Carlos Martinez T

Just in case somebody else is still looking for a solution to this. Try this when the page loads.

以防万一其他人仍在寻找解决方案。当页面加载时试试这个。

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

回答by Philippe

If I understand correctly,

如果我理解正确,

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

clicking the above link in a browser normally results in an added hash in the address bar, like www.websitename.com#someElementID<- this is what you're looking to prevent, yes?

在浏览器中单击上面的链接通常会在地址栏中添加一个哈希值,例如www.websitename.com#someElementID<- 这就是您要防止的,是吗?

The solution I just tested which is working and DOES NOT refresh the page is:

我刚刚测试的解决方案是有效的并且不刷新页面是:

event.preventDefault();

This works in a 'click()' event that involves anchor tags that link to elements' id's, such as in the anchor tag example above. In action, it would look like this in your 'click()' event:

这适用于涉及链接到元素 id 的锚标记的“click()”事件,例如在上面的锚标记示例中。实际上,它在您的 'click()' 事件中看起来像这样:

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

Now, clicking on the same link leaves the address bar with the same URL www.websitename.com, WITHOUT the added hash from clicking the anchor.

现在,单击相同的链接会留下具有相同 URL www.websitename.com 的地址栏,而没有通过单击锚点添加的哈希值。

回答by Saravanan Sivam

We can remove/escape from appending the hash by returning false in click function.

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

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

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

回答by IBRAHIM EZZAT

you will find this example from w3schoolsvery useful for your need plus it gives you elegant scrolling move compatable with all browsers, check below code :

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

  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {

    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area

      $('html, body').animate({
          scrollTop: $(hash).offset().top
       }, 800, function(){

      // Add hash (#) to URL when done scrolling (default click behavior)
      // obvously you will ignore this step because you want to remove the hash in first place - but just in case you want turn it on again

      window.location.hash = hash;
    });
   } // End if
  });
});