Fixing your skip links. [JS]
(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]);
(function(a,b,c,d) {
for (c in a) {
if ((d=a[c].hash)&&a[c].href==b+d&&a[c].addEventListener) {
a[c].addEventListener('click',function(e,f,g,h) {
if (e=(f=document).getElementById(g=this.hash.slice(1))||f.getElementsByName(g)[0]) {
if (h=!e.getAttribute(f='tabindex')) e.setAttribute(f,-1);
e.focus();
if (h) e.removeAttribute(f);
}
});
}
}
})(document.links,location.href.split('#')[0]);
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Read Damon Muma on this. He proposes the following jQuery solution (inspired by Thompson, fixed by me):
// Apply focus properly when accessing internal links with keyboard in WebKit browsers.
$("a[href^='#']").not("a[href='#']").click(function() {
$("#"+$(this).attr("href").slice(1)+"").focus();
});
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.
tabindex
now using -1
instead of 0
, as per Smith, added this README, and added UNLICENCE. (c194df)tabindex
values. (a39c9b)name
attribute is obsolete and is only checked for backwards compatibility. (0ab22c)#
) or undefined elements (#thisIDisnotused
). (9ebb30)#
but starting with the current page’s location. Also updated the README with a list of tested browsers. (8adf2a)BASE
element. As reported by Gartner. (prev. b8648a) (341fdf)onClick
events. (376b75)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)