Skip to content

Instantly share code, notes, and snippets.

@Dykam
Forked from Zegnat/README.md
Created February 28, 2012 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dykam/1934449 to your computer and use it in GitHub Desktop.
Save Dykam/1934449 to your computer and use it in GitHub Desktop.
Fixing your skip links. [JS]

Fixing your skip links.

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.

Version history.

  1. Pilot version. (d385d2)
  2. tabindex now using -1 instead of 0, as per Smith, added this README, and added UNLICENCE. (c194df)
  3. Updates to the README. (prev. 0fce31) (prev. a31368) (7d175b)
  4. Added a clause to check for pre-existing tabindex values. (a39c9b)
  5. Added support for named anchors. The name attribute is obsolete and is only checked for backwards compatibility. (0ab22c)
  6. No more errors on empty fragments (#) or undefined elements (#thisIDisnotused). (9ebb30)
  7. Added support for URLs not starting with # but starting with the current page’s location. Also updated the README with a list of tested browsers. (8adf2a)
  8. Shaved off some bytes and added a compressed version. (116262)
  9. Updated the README, it is no longer just a quick port. (924088)
  10. Even smaller. (prev. f6cf48) (aeb909)
  11. Fix for pages using a BASE element. As reported by Gartner. (prev. b8648a) (341fdf)
  12. Current: Stop overwriting existing onClick events.
  13. Ported to coffeescript, added compressed and compiled versions as well. The compiled and compressed javascript is just slightly larger than the compressed javascript. Further nibbled 2 chars of the original javascript using the closure compiler.

Tested browsers

  • Firefox 10.0.2
  • Chrome 17.0.963.56
  • Safari 5.0.6 (5533.22.3)
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/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment