Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created February 24, 2012 12:03
Show Gist options
  • Save Zegnat/1900563 to your computer and use it in GitHub Desktop.
Save Zegnat/1900563 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, and have been trying to port it to plain Javascript.

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. (0fce31)
  4. Added file extension to README. Oops. (a31368)
  5. Last change to README. I hope. (7d175b)
  6. Added a clause to check for pre-existing tabindex values. (a39c9b)
  7. Added support for named anchors. The name attribute is obsolete and is only checked for backwards compatibility. (0ab22c)
  8. No more errors on empty fragments (#) or undefined elements (#thisIDisnotused). (9ebb30)
  9. Current: 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.

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/>
@komputist
Copy link

Hi! Would it be possible to extend this method so that links followed from the longdesc attribute or the cite attribute would also change focus? For a Webkit browser that allows you to open @longdesc URLs and @cite URLs, see iCab. http://www.icab.de Longdesc attribute URLs and cite attribute URL are, in iCab, opened via a contextual menu. I have a test page here: http://malform.no/testing/a-demo-of/longdesc-with-hidden-iframe/

@pmgodin-zz
Copy link

I preferred the native javascript version since the jQuery one may interfere with deeplinking "/#/deeplink" syntax because "/" cannot be used as jQuery selector.

@Zegnat
Copy link
Author

Zegnat commented Oct 10, 2012

@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.

Maybe I can couple this to the onhashchange event, but no promises.

@Zegnat
Copy link
Author

Zegnat commented Oct 10, 2012

@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.

Maybe I can couple this to the onhashchange event, but no promises.

@virtualgeoff
Copy link

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.

In the end I combined both approaches:

$('.skiplinks a').click(function() {
    $("#" + $(this).attr("href").slice(1)).attr("tabindex",-1).focus().removeAttr("tabindex");
});

@simeydotme
Copy link

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.

@Veyfeyken
Copy link

This breaks the back-button of the browser. More info and fix on WebAIM's e-mail list:
http://webaim.org/discussion/mail_thread?thread=6363

Also, please vote for a webkit fix:

https://bugs.webkit.org/show_bug.cgi?id=116046
https://code.google.com/p/chromium/issues/detail?id=262171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment