Remove hash from window.location in Javascript

0 min read 80 words

E.g. URL:<br>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("#")[0]

or

window.location.href.substr(0, window.location.href.indexOf("#"))

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("#")[0];

or

window.location = window.location.href.substr(0, window.location.href.indexOf("#"));
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags