2

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?

David Jones
  • 10,117
  • 28
  • 91
  • 139
  • Bummer - I wonder why the browser doesn't realize when it's already on the correct page? – David Jones Mar 03 '12 at 06:05
  • @danielfaraday Anchors within a page work fine: http://jsfiddle.net/ZLe7N/. Are you sure that your current page is `/home`, and not, say`/Home` or `/home?bla=bla`? – Rob W Mar 03 '12 at 08:41
  • Rob, the question is this: can you have a link to an internal anchor that works from another page and doesn't trigger a reload when used from the current page... – David Jones Mar 03 '12 at 22:24

3 Answers3

7

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.

David Jones
  • 10,117
  • 28
  • 91
  • 139
1

For anyone wondering how to do this for a WordPress homepage, I found just using a forward slash with nothing in front of it works:

/#anchor
0

If you are using the Polylang plugin and slug format of url's, then add just:

/en/somepage/#something or /fr/somepage/#something etc.

to avoid reloading

marikamitsos
  • 10,264
  • 20
  • 26
Pavel
  • 1