<a> element is invalid HTML unless it has either an
If you want it to render correctly as a link (ie underlined, hand pointer, etc), then it will only do so if it has a
Code like this is therefore sometimes used as a way of making a link, but without having to provide an actual URL in the
href attribute. The developer obviously wanted the link itself not to do anything, and this was the easiest way he knew.
<a> tag link.
Some developers use
href='#' for the same purpose, but this causes the browser to jump to the top of the page, which may not be wanted. And he couldn’t simply leave the href blank, because
href='' is a link back to the current page (ie it causes a page refresh).
href is one of them, and although it isn’t the best solution, it does work.
clicking the link will fire the alert.
There are several mechanisms to avoid a link to reach its destination. The one from the question is not much intuitive.
A cleaner option is to use
#no is a non-defined anchor in the document.
You can use a more semantic name such as #disable, or #action to increase readability.
Benefits of the approach:
- Avoids the “moving to the top” effect of the empty href=”#”
- You must be sure the anchor name is not used in the document.
<a> element is not acting as a link, the best option in these cases is not using an
<a> element but a
<div> and provide the desired link-like style.
is just shorthand for:
<a href="" onclick="alert('Hello'); return false;"></a>
Refer to this: