Skip to content

Remove hash from window.location in Javascript

E.g. URL:
http://example.com/?option=1&task=2&listing_id=36&layout=4&table=5#some_hash_value_here

So how would you get the current URL using Javascript?

That is really easy, you can use window.location.href or just simply window.location.

But how do you replace the #hash at the end if it exists?

That too is quite easy, you can just do something like this:

window.location.href.split("#")[]
Code language: JavaScript (javascript)

or

window.location.href.substr(, window.location.href.indexOf("#"))
Code language: JavaScript (javascript)

So now that we have the value of the current URL how do we refresh the page with it?

window.location = window.location.href.split("#")[];
Code language: JavaScript (javascript)

or

window.location = window.location.href.substr(, window.location.href.indexOf("#"));
Code language: JavaScript (javascript)

See also  How to Create a Hashtag Generator in Javascript
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x