skip-link-focus-fix.js 792 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * File skip-link-focus-fix.js.
  3. *
  4. * Helps with accessibility for keyboard only users.
  5. *
  6. * This is the source file for what is minified in the twentytwenty_skip_link_focus_fix() PHP function.
  7. *
  8. * Learn more: https://git.io/vWdr2
  9. */
  10. ( function() {
  11. var isIe = /(trident|msie)/i.test( navigator.userAgent );
  12. if ( 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. }() );