skip-link-focus-fix.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * File skip-link-focus-fix.js
  3. *
  4. * Helps with accessibility for keyboard only users.
  5. * This is the source file for what is minified in the astra_skip_link_focus_fix() PHP function.
  6. *
  7. * Learn more: https://github.com/Automattic/_s/pull/136
  8. *
  9. * @package Astra
  10. */
  11. ( function() {
  12. var is_webkit = navigator.userAgent.toLowerCase().indexOf( 'webkit' ) > -1,
  13. is_opera = navigator.userAgent.toLowerCase().indexOf( 'opera' ) > -1,
  14. is_ie = navigator.userAgent.toLowerCase().indexOf( 'msie' ) > -1;
  15. if ( ( is_webkit || is_opera || is_ie ) && document.getElementById && window.addEventListener ) {
  16. window.addEventListener( 'hashchange', function() {
  17. var id = location.hash.substring( 1 ),
  18. element;
  19. if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
  20. return;
  21. }
  22. element = document.getElementById( id );
  23. if ( element ) {
  24. if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) {
  25. element.tabIndex = -1;
  26. }
  27. element.focus();
  28. }
  29. }, false );
  30. }
  31. })();