Jul/092
Quick Tip: don’t use links with JavaScript unless the link goes some where
We have all done it, many times I am sure. You need an element to attach some JavaScript to, that will execute some function, so you use <a href=”#” onclick=”myfunction(); return false;”>click me</a>. The link does not go any where so we take steps to prevent the link from actually going any where. I say don’t use a link, use any other DOM element instead and style it like a link, if you actually want it to look like a link.
1 2 3 4 5 6 7 8 9 |
Click Here Clear
I can think of one situation that you would actually want to use links and that is if you want the link to go some where in the case that JavaScript is disabled. You could for instance take users to a page detailing the need for JavaScript for the page to function properly with instructions to enable JavaScript.
Enjoy this article?
Sorry, the comment form is closed at this time.
No trackbacks yet.
5:54 pm on July 9th, 2009
While I totally agree with you in principal, links also cause actions on pages (namely, moving to named anchors). Similarly, they can be used to move content, like tabs or sliders. For those instances, links with a bit of javascript aid makes sense. Where they don’t make sense is wrapping a button image in an .
6:04 pm on July 9th, 2009
@Paul I agree with you when it is an anchor, as it indeed goes some where. I am not sure that I agree with the “tabs or sliders” bit. Could you expand on how a link is more useful in the case of a tab or slider than say a or ? I have run into so many situations where a link with just a hash and some JavaScript ends up scrolling the page to the top of the page, there by taking the user away from the control they were interacting with. A specific case was a control that had been working fine until IE8 came out, and something about the JS, the HTML and IE8 made it follow the anchor.