3

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!

Zeca Novaes
  • 379
  • 1
  • 2
  • 17

1 Answers1

3

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/"
smottt
  • 3,272
  • 11
  • 37
  • 44
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • 1
    Thanks, Chris! With hash = ' ' I can go back too. The problem is the hash character (#) in the url. My home page link is like mysite.com/# – Zeca Novaes Feb 20 '16 at 00:23
  • Yea no problem @ZecaNovaes! Dont forget to mark the question best anwser if it helped you solve the problem to help future users of stackoverflow! – amanuel2 Feb 20 '16 at 00:24
  • Sure, thank you! I still trying to find any way to remove this hash in my URL, but your answer can help me too! – Zeca Novaes Feb 20 '16 at 00:27
  • Edit @ZecaNovaes i will anwser that question shortly – amanuel2 Feb 20 '16 at 00:30
  • Sorry for the "Chris" name, I couldn't see the "edited by" before haha. I'll edit the post for you! – Zeca Novaes Feb 20 '16 at 00:33
  • 1
    @ZecaNovaes Edited my anwser to the solution to your problem. Did it help? – amanuel2 Feb 20 '16 at 00:39
  • The problem in change the href is the redirection. I use this hashs to change the page without refreshing, is like an anchor-based site. I'll accept your answer 'cause you are the first person I saw in SO who go back and edit your own answer trying to help. Thank you! – Zeca Novaes Feb 20 '16 at 00:43
  • 1
    Hmm @ZecaNovaes im very sorry i cant solve your problem! I just dont seem to work without refreshes. Ill notify you if i get a solution – amanuel2 Feb 20 '16 at 01:01
  • No problem! If i get it, I'll post here too! Thank you again! – Zeca Novaes Feb 20 '16 at 02:28