Days ago, I shared a conversation with a man named Jeff. We met in an alley off the a lesser-known junction of the Canadian arterial internet pipe.
I was afraid, at first, as he is tall and wildly bearded. But he was not an electronic mugger, and wished instead to discuss an issue encountered while using XHTML Strict.
This was convenient for me, for I had no wish to be mugged, and he said: When clicking on links to #, you are jumped to the top of the page.
<a href="#" onclick="do_some_js_stuff();">JS Interface link</a>
I agreed with him, not yet convinced his intentions completely mug-free, and suggested returning false.
<a href="#" onclick="do_some_js_stuff(); return false;">JS Interface link</a>
Dawn found my words effective, as I left that alley with both wallet and dignity.
But the experience got me thinking. I would prefer an internet better lit, and I might approach it like this:
<a href="#" class="jsui" onclick="do_some_js_stuff();">JS Interface link</a>
And with jquery, use the jsui class to automatically return false on clicks:
$('.jsui').click( function() { return false;});
An other option would be to return the return value of the event handler you’re already using:
<a href="#" onclick="return do_some_js_stuff();">JS Interface link</a>
And just be sure to return false from applicable handlers.
In summary, when clicking on a link to # while using XHTML Strict, you will be jumped to the beginning of the page. We can combat this by returning false. There are many ways you can do that, but which you use probably only matters if you are a snob.
Disappointment.
🙁
I like this post because I probably would’ve been really annoyed by that at some point. Now instead I will scoff at the internet as I subdue it.