skip-link-focus-fix.js 880 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * File skip-link-focus-fix.js.
  3. *
  4. * Helps with accessibility for keyboard only users.
  5. *
  6. * Learn more: https://git.io/vWdr2
  7. */
  8. ( function() {
  9. var isWebkit = navigator.userAgent.toLowerCase().indexOf( 'webkit' ) > -1,
  10. isOpera = navigator.userAgent.toLowerCase().indexOf( 'opera' ) > -1,
  11. isIe = navigator.userAgent.toLowerCase().indexOf( 'msie' ) > -1;
  12. if ( ( isWebkit || isOpera || isIe ) && document.getElementById && window.addEventListener ) {
  13. window.addEventListener( 'hashchange', function() {
  14. var id = location.hash.substring( 1 ),
  15. element;
  16. if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
  17. return;
  18. }
  19. element = document.getElementById( id );
  20. if ( element ) {
  21. if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) {
  22. element.tabIndex = -1;
  23. }
  24. element.focus();
  25. }
  26. }, false );
  27. }
  28. })();