poporo
poporo

Reputation: 51

How to replace hash in url

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.

Upvotes: 5

Views: 16156

Answers (4)

PayteR
PayteR

Reputation: 1757

in my opinion is using regex replace on string best and cleanest solution

.replace( /#.*/, "");

example

location.href = location.href.replace( /#.*/, "");

Upvotes: 0

Santosh Suryavanshi
Santosh Suryavanshi

Reputation: 1

var file = location.pathname.substring(location.pathname.lastIndexOf("/") + 2);
var location = window.origin + "file";
window.location = location;

Upvotes: 0

vol7ron
vol7ron

Reputation: 42095

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');

Upvotes: 1

Joseph Marikle
Joseph Marikle

Reputation: 78520

location.href = location.href.replace(location.hash,location.hash.substr(1))

Upvotes: 7

Related Questions