home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-content / themes / twentysixteen / js / skip-link-focus-fix.js < prev   
Encoding:
Text File  |  2017-05-30  |  1.0 KB  |  37 lines

  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.  
  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.  
  13.     if ( ( isWebkit || isOpera || isIE ) && document.getElementById && window.addEventListener ) {
  14.         window.addEventListener( 'hashchange', function() {
  15.             var id = location.hash.substring( 1 ),
  16.                 element;
  17.  
  18.             if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
  19.                 return;
  20.             }
  21.  
  22.             element = document.getElementById( id );
  23.  
  24.             if ( element ) {
  25.                 if ( ! ( /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) ) {
  26.                     element.tabIndex = -1;
  27.                 }
  28.  
  29.                 element.focus();
  30.  
  31.                 // Repositions the window on jump-to-anchor to account for admin bar and border height.
  32.                 window.scrollBy( 0, -53 );
  33.             }
  34.         }, false );
  35.     }
  36. } )();
  37.