skip-link-focus.js 683 B

12345678910111213141516171819202122232425262728293031
  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 isIe = /(trident|msie)/i.test( navigator.userAgent );
  10. if ( isIe && document.getElementById && window.addEventListener ) {
  11. window.addEventListener( 'hashchange', function() {
  12. var id = location.hash.substring( 1 ),
  13. element;
  14. if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
  15. return;
  16. }
  17. element = document.getElementById( id );
  18. if ( element ) {
  19. if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) {
  20. element.tabIndex = -1;
  21. }
  22. element.focus();
  23. }
  24. }, false );
  25. }
  26. })();