In playing around with ways to attach events to <a> elements without triggering the default behavior (mainly when href is ”#”), I came up with a ‘cute’ way to handle it (simply using preventDefault(), but in a somewhat creative way).
The reasons I have for doing it this way are:
1) I’m working with existing code that uses href=”#” pretty much everywhere an event needs to fire on an <a> element
to make it easier to find these Not Links™ when quickly scanning the code.
3) to make my IDE happy (IntelliJ IDEA) so it doesn’t criticize my code and highlight it and call it names (I take it very personally)
So, here’s what I do:
1) Use href=”##” instead of just href=”#”. This makes these Not Links™ stand out a little more, plus I’ll know which ones I put in there since the existing code just uses ”#”. You could really use anything (like ”*” or “!” or be like the cool kids and use “!#”), but if the href at least starts with ”#” then the browser won’t even attempt to navigate away from the current page.
Here’s the jQuery code (on one line):
What do you think? Wast of time or useful technique? Or a moderately somewhat useful technique that’s really a waste of time (much like writing this blog post)?