Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace hash in url

Tags:

javascript

How to replace a url like this

http://test.com/#part1

to:

http://test.com/part1

I know location.hash but it will detect if there is a hash in url.

like image 322
poporo Avatar asked Aug 05 '11 03:08

poporo


People also ask

How do I change the URL of a hash?

Change hash of the current url URL is the current url var url_ob = new URL(document. URL); url_ob. hash = '#345'; // new url var new_url = url_ob. href; // change the current url document.

How do I remove a hash from a link?

To remove the hash URL, you can use the replaceState method on the history API to remove the hash location. Example: HTML.

Why is there a hash in my URL?

In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. When you use a URL with a # , it doesn't always go to the correct part of the page or website.

Which part of the URL is hash?

The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document.


2 Answers

location.href = location.href.replace(location.hash,location.hash.substr(1))
like image 145
Joseph Marikle Avatar answered Sep 20 '22 03:09

Joseph Marikle


You can use replace()

Here's a broken down version using windows.location:

var new_url = window.location.protocol + '//'
            + window.location.hostname + '/'
            + window.location.pathname + '/'
            + window.location.hash.replace('#','','g') ;

Or remove all the hashes:

var new_url = (window.location + '').replace('#','','g');
like image 23
vol7ron Avatar answered Sep 20 '22 03:09

vol7ron