I don’t like to use jQuery however for things that should be seen as required code for any website. Below I include my own JavaScript port, including some error checking and more complete HREF support.

Inspired by Duffy’s take on things it now uses addEventListener instead of onClick to avoid event clashes, and the complete location value instead of a virtual A-element. Also fine-tuned it to not apply to too many wrong links and shortened the code some more. (05bff0)

(function(a,b,c,d){for(c in a)(d=a[c].hash)&&a[c].href==b+d&&a[c].addEventListener&&a[c].addEventListener("click",function(a,b,c,d){if(a=(b=document).getElementById(c=this.hash.slice(1))||b.getElementsByName(c)[0])(d=!a.getAttribute(b="tabindex"))&&a.setAttribute(b,-1),a.focus(),d&&a.removeAttribute(b)})})(document.links,location.href.split("#")[0]);

@komputist sorry for missing your comment. I’ll try to look into it but I can’t promise anything. It’s easy to bind to an onClick event and I don’t expect iCab to trigger a JavaScript event because you want to display the longdesc.

@komputist sorry for missing your comment. I’ll try to look into it but I can’t promise anything. It’s easy to bind to an onClick event and I don’t expect iCab to trigger a JavaScript event because you want to display the longdesc.

The jQuery solution above doesn't work if the targets are not actual links i.e. if they are not able to accept focus. So I tried your pure JS solution but it interferred with some in-page scrolling I have that also uses fragment identifiers.

Thank you so much for this, works perfectly for me!
I think this should be included in the HTML5 Boilerplate , it's as important as .visuallyhidden and chromeframe, can't believe I didn't realise this was broken sooner.