-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathskip-link-focus.js
More file actions
78 lines (64 loc) · 1.82 KB
/
Copy pathskip-link-focus.js
File metadata and controls
78 lines (64 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Skip Link Focus v1.0.0
* https://github.com/cedaro/skip-link-focus
*
* @copyright Modifications Copyright (c) 2015 Cedaro, LLC
* @license BSD-3-Clause
*/
/**
* Make "skip to content" links work correctly in IE9, Chrome, and Opera to
* improve accessibility.
*
* @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
*/
(function( root, factory ) {
'use strict';
if ( 'function' === typeof define && define.amd ) {
define( [], factory );
} else if ( 'object' === typeof exports ) {
module.exports = factory();
} else {
root.skipLinkFocus = factory();
}
}( this, function() {
'use strict';
function init( options ) {
options = options || {};
options.selector = options.selector || '.skip-link';
if ( window && /webkit|opera|msie|trident/i.test( navigator.userAgent ) && window.addEventListener ) {
var i,
skipLinks = window.document.querySelectorAll( options.selector );
window.addEventListener( 'hashchange', function() {
skipToElement( location.hash.substring( 1 ) );
}, false );
// Fix for when the address bar already contains a hash.
for ( i = 0; i < skipLinks.length; ++i ) {
skipLinks[ i ].addEventListener( 'click', skipLinkClickHandler );
}
// Handle initial hash.
if ( location.hash && location.hash.substring( 1 ) ) {
skipToElement( location.hash.substring( 1 ) );
}
}
}
function skipLinkClickHandler( e ) {
skipToElement( e.target.hash.substring( 1 ) );
}
function skipToElement( id ) {
var element;
if ( ! ( /^[A-z0-9_-]+$/.test( id ) ) ) {
return;
}
element = window.document.getElementById( id );
if ( element ) {
if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) {
element.tabIndex = -1;
}
element.focus();
}
}
return {
init: init,
skipToElement: skipToElement
};
}));