On many websites I see links that have href=\"#\"
. What does it mean? What is it used for?
Unfortunately, the most common use of is by lazy programmers who want clickable non-hyperlink javascript-coded elements that behave like anchors, but they can't be arsed to add
cursor: pointer;
or :hover
styles to a class for their non-hyperlink elements, and are additionally too lazy to set href
to javascript:void(0);
.
The problem with this is that one or another inevitably ends up with a javascript error, and an anchor with an onclick javascript error always ends up following its
href
. Normally this ends up being an annoying jump to the top of the page, but in the case of sites using
, is handled as
, resulting in an unexpected navigation. If any logable errors are being generated, you won't see them in the latter case unless you enable persistent logs.
If an anchor element is used as a non-anchor it should have its href
set to javascript:void(0);
for the sake of graceful degradation.
I just wasted two days debugging a random unexpected page redirect that should have simply refreshed the page, and finally tracked it down to a function raising the click event of an . Replacing the
#
with javascript:void(0);
fixed it.
The first thing I'm doing Monday is purging the project of all instances of .