Sam Chahine
Sam Chahine

Reputation: 620

Hiding the hash location in JavaScript

I have a page with links that open a modal with id="modal-i (where i is any number) linked to an anchor tag with href="#modal-i". This works fine, you click the link and the associated modal will pop up, then when a link with href="#" is clicked the modal disappears.

I was wondering if there was a way to hide the hash location but keep the modal open? Basically keep everything working the same and just hide the hash in the URL.

I know that the only reason the modal will pop up is BECAUSE the hash is in the URL, but still wondering if it was possible, as it would make everything much cleaner.

I have searched for the answer but all I have found are questions asking how to remove the hash location with no page-reload.

Any help is greatly appreciated, thank you!

Upvotes: 1

Views: 125

Answers (1)

Trevor Clarke
Trevor Clarke

Reputation: 1478

Well, sort of. You can remove it instantly.

Put this somewhere on your page:

<script>
document.body.onload = function() {updateurl()};
function updateurl(){

window.history.replaceState(window.location.hostname, "Sample Title", window.location.pathname);
}
</script>

All you need to do is change "Sample Title" to whatever you want.

Good luck

Upvotes: 2

Related Questions