skip-link-focus-fix.js 935 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Makes "skip to content" link work correctly in IE9, Chrome, and Opera
  3. * for better accessibility.
  4. *
  5. * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
  6. */
  7. ( function() {
  8. var isWebkit = navigator.userAgent.toLowerCase().indexOf( 'webkit' ) > -1,
  9. isOpera = navigator.userAgent.toLowerCase().indexOf( 'opera' ) > -1,
  10. isIE = navigator.userAgent.toLowerCase().indexOf( 'msie' ) > -1;
  11. if ( ( isWebkit || isOpera || isIE ) && document.getElementById && window.addEventListener ) {
  12. window.addEventListener( 'hashchange', function() {
  13. var id = location.hash.substring( 1 ),
  14. element;
  15. if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
  16. return;
  17. }
  18. element = document.getElementById( id );
  19. if ( element ) {
  20. if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) {
  21. element.tabIndex = -1;
  22. }
  23. element.focus();
  24. }
  25. }, false );
  26. }
  27. } )();