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.
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.
To remove the hash URL, you can use the replaceState method on the history API to remove the hash location. Example: HTML.
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.
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.
location.href = location.href.replace(location.hash,location.hash.substr(1))
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With