I have a react SPA where I have some links which require javascript to change the page url. Semantically these should be links, but the link destination is computed at runtime on click, or it requires javascript to move to that location (history api)
I have seen it mentioned at a few places that href="#" is an antipattern or something that should be avoided. For example this article from webaim https://webaim.org/techniques/hypertext/ mentions that:
One of the most serious barriers is to create links that go nowhere. Developers sometimes use JavaScript to create dynamic menus that drop down when the user hovers over certain links with the mouse. In some cases, the link itself goes nowhere at all, and its only purpose is to expose the links in the drop-down menu, which do have real destinations. Links like this often have a pound sign as the link destination, which means that the link destination is the same page; clicking on the link accomplishes nothing. Both keyboard users and mouse users will experience nothing at all when attempting to activate the link.
and it gives an example of href="#"
Now i get that using href="#" for links which do nothing or do not take the user anywhere might semantically be incorrect.
In my usage though, the link does have a destination, just that the destination can't be specified in href and may not be decided unless the link is clicked. So my usage is to have href="#"
and e.preventDefault()
onClick, so that it does not go to top of the page and create another history entry.
I have seen other working solutions/workarounds for this:
using hash vs javascript:void(0) Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?
other workarounds https://weblog.west-wind.com/posts/2019/Jan/21/NonNavigating-Links-for-JavaScript-Handling
But i would like to understand, if using href="#"
for links that do have a destination (which is figured out after clicking on the link, or applied using javascript) is an antipattern.
This is a follow up to this question related to javascript only links accessibility Accessibility: Using javascript only links with href="#"