Skip to content
Advertisement

href / anchor (jump to id) weird behavior in Github pages

Please take a look at my page, https://kuncung38.github.io/portfolio-website/

When I used live server from VSCode, everything works just as expected, When I uploaded this to github pages, a weird bug appeared.

You will notice the problem, it has a weird behavior when you clicked either button, you will have to clicked the button twice for something to happen. Here is my code:

JavaScript

Basically the two buttons on the homepage have these two functions. I do not know what is wrong with my codes, You can inspect the webpage to see the full code if needed, or you can click here.

Please help me 🙁

Advertisement

Answer

The first time you click, index.html is added to the URL, so effectively you navigate away from the current page. If you start out at https://kuncung38.github.io/portfolio-website/index.html the problem disappears.

A solution would be not to overwrite the entire window.location.href but only the hash:

JavaScript

That should work whether or not index.html is in the URL.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement