Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fixing your skip links. [JS]
(function(a,b,c){for(b=a.length;b--;)if(a[b].getAttribute('href').substr(0,1)=='#')a[b].onclick=function(c){c=document.getElementById(this.hash.substr(1));c.setAttribute('tabindex','0');c.focus();c.removeAttribute('tabindex');};})(document.links);
@komputist

This comment has been minimized.

Show comment
Hide comment
@komputist

komputist Sep 16, 2012

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/

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

This comment has been minimized.

Show comment
Hide comment
@pmgodin-zz

pmgodin-zz Oct 2, 2012

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

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

@Zegnat

This comment has been minimized.

Show comment
Hide comment
@Zegnat

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

Owner

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

This comment has been minimized.

Show comment
Hide comment
@Zegnat

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

Owner

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

This comment has been minimized.

Show comment
Hide comment
@virtualgeoff

virtualgeoff Feb 18, 2013

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");
});

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

This comment has been minimized.

Show comment
Hide comment
@simeydotme

simeydotme Mar 20, 2013

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.

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

This comment has been minimized.

Show comment
Hide comment
@Veyfeyken

Veyfeyken Mar 24, 2014

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

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