Zeca Novaes
Zeca Novaes

Reputation: 419

Remove URL hash (not as variable) for hashchange event

I'm working with jQuery hashchange event, and every button in my page uses this code like a link to another page:

window.location.hash = '#anotherpage';

When I need to go back to the homepage, location.hash leaves the hash character (#) in my URL, even with location.hash = null or location.hash = ""

Does anyone here know how can I remove the hash in this case?

EDIT:

As Dsafds and Daniel said, only go back to my home page can be easy if I use window.location.hash = "#". The problem is the character (#) in the home page link, used for the other pages, like #contact, is normal, but my home page have the hash too (site.com/#). I need to remove it. Any other idea? Thanks!

Upvotes: 3

Views: 1306

Answers (1)

amanuel2
amanuel2

Reputation: 4646

You can just use this to go back to the home page:

window.location.hash="/" || window.location.hash="/home"

In your case:

window.location.hash = "#"

EDIT

I made a workaround for the problem you were having.. on removing the hashes.

So what you can do is set the Full Url when you change "locations". For example this:

Lets say you want to go to the login page:

window.location.href = "https://mysite/#/login"

Now you want to go back to the home page! Then you can do this:

window.location.href = "https://mysite/"

Upvotes: 3

Related Questions