-5

How do I remove a URL's hash sign and the text after it?

For example, the URL is http://www.website.com/home#content

I want the whole #content text to be removed.

Makarov Sergey
  • 932
  • 7
  • 21
  • `document.location.hash = ""`, and yes, it's called *"the hash"*. – adeneo Oct 10 '16 at 18:46
  • 2
    Get the Hash: http://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript, Remove It: http://stackoverflow.com/questions/1397329/how-to-remove-the-hash-from-window-location-with-javascript-without-page-refresh – GlabbichRulz Oct 10 '16 at 18:47
  • Possible duplicate of [How to remove the hash from window.location (URL) with JavaScript without page refresh?](https://stackoverflow.com/questions/1397329/how-to-remove-the-hash-from-window-location-url-with-javascript-without-page-r) – PayteR Sep 10 '17 at 12:55

2 Answers2

0
window.location.href.split('#')[0]  

You can try that!

Nihal Rp
  • 484
  • 6
  • 15
0

I think this is what you're looking for... (i.e. when you click a hyperlink that has a hash in its href, you want to strip out the hash and navigate to the remaining URL?)

<!DOCTYPE html>
<html>
    <body>
        <a id="someLink" href="/some_page#some_hash">Click Me</a>
        <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script>
            $('#someLink').click(function (e) {
                // Prevent normal navigation to the href (the full URL with hash)
                e.preventDefault();
                // Navigate to "/some_page" (everything to left of the first "#")
                document.location = this.href.split('#')[0];
            });
        </script>
    </body>
</html>
Jeff McCloud
  • 5,777
  • 1
  • 19
  • 21