Tuesday, November 9, 2010

JavaScript links

Saw this interesting discussion about whether we should use href="javascript:void(0)" or href="#" with onClick event handler on Stack Overflow.

I think the best solution is to avoid using link element <a> in the first place and use CSS to style an element to make it "look" like a link and attach the event handler to that element. This article has more discussions and comparisons why we should not use links in this particular case.

As for why the event handler need to return false, it is because we want to avoid the default behavior of the  link element, i.e. to follow the href and go to another page. This quirksmode article has it very clearly explained.

No comments:

Post a Comment