Search code examples
htmlhyperlinkanchorreloading

HTML anchors that work from other pages without reloading the host page


My navigation menu is generated in a single PHP file and included in each page. Some of the links are simply anchors on the homepage. When the user is on another page, these links should take them to the home page and then to the appropriate anchor. However, if the user is already on the home page, it would be nice if the page didn't have to reload.

This will reload no matter what:

<a href="/home#anchor">link</a>

This won't reload if you're already on the home page, but doesn't work from other pages:

<a href="#anchor">link</a>

Is there a way to achieve the best of both worlds without using JavaScript?


Solution

  • Okay I figured this out. What I had was a folder called home. So if you navigated to http://example.com/home it would take you to index.php in the home folder. I also had an internal anchor on index.php. If the navigation link had /home#anchor as the HREF, then the page would reload when you clicked on it. What I discovered is that if the link has /home/#anchor (with an extra forward slash), then the page doesn't reload when the link is clicked. Hope this helps someone else.