You should either return false;
from the event handler of A
tag
Or, use
<a href="javascript: void(0);" class="dosomething">Click Me</a>
For those who thinks javascript: void(0)
is bad practice
If you use href='#'
, you must take care of two things
// one
function fn() {
// code
return false;
}
// two
<a href="#" onclick="return fn();">click</a>
And if you forget and just write onclick="fn();"
it won't work
Another thing why I used javascript: void(0);
is, if the function encounters/throws an error, it wont return false
So if you're a lone developer then you can clearly make your own choice, but if you work as a team you have to either state:
Use href="#", make sure onclick always contains return false; at the end, that any called function does not throw an error and if you attach a function dynamically to the onclick property make sure that as well as not throwing an error it returns false.
OR
Use href="javascript:void(0)"
Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?