This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.announcethis.js
84 lines (73 loc) · 3.03 KB
/
jquery.announcethis.js
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
79
80
81
82
83
84
/*
* announceThis v0.5
* @desc Screenreader messaging plug-in for jQuery
* @author Jason Day
*
* Licensed under the MIT licence:
* http://www.opensource.org/licenses/mit-license.php
*
* (c) Jason Day 2016
*
* Usage:
*
* $.announceThis({
* id: 'announceThis', // id of live region
* role: 'log', // log, alert, status, timer
* ariaLive: 'assertive', // polite, assertive, alert (automatically becomes "alert" when role: "alert")
* ariaAtomic: false, // present live region as a whole (cannot be used with aria-relevant)
* ariaRelevant: '', // 'additions', 'additions removals', 'removals' - does not work with role: alert
* message: '' // message to pass to screenreader
* });
*
* Notes:
* - When using the same ID, the first instance of options will be the options used
* - role: 'alert' ignores all options except for message
*/
;
(function($) {
var opt;
$.announceThis = function(options) {
opt = $.extend({}, $.announceThis.defaults, options);
if(opt.role == "alert"){
// maintain only one instance of alert at a time, opt.id is not reflected here
$('#announceAlert').remove();
// create container
var announcement = "<div id='announceAlert' class='announceThis' role='alert' aria-live='assertive'></div>";
// append to page
$('body').append(announcement);
// delay allows live regions to be built dynamically and announced
setTimeout(function(){
$("#announceAlert").append("<p>" + opt.message + "</p>");
}, 250);
} else {
// if element has not been created, create it
if(!$("#" + opt.id).length){
var announcement = "<div id=" + opt.id;
announcement += " class='announceThis'";
announcement += " role=" + opt.role;
announcement += " aria-live=" + opt.ariaLive;
if(opt.ariaAtomic){
announcement += " aria-atomic=" + opt.ariaAtomic;
}
if(opt.ariaRelevant){
announcement += " aria-relevant='" + opt.ariaRelevant + "'";
}
announcement += "></div>";
// append to page
$('body').append(announcement);
}
// delay allows live regions to be built dynamically and announced
setTimeout(function(){
$("#" + opt.id).append("<p>" + opt.message + "</p>");
}, 250);
}
};
$.announceThis.defaults = {
id: "announceThis", // id of live region
role: "status", // log, alert, status, progressbar, marquee, timer
ariaLive: "assertive", // polite, assertive, alert (automatically becomes "alert" when role: "alert")
ariaAtomic: false, // present live region as a whole (support across screenreader/browser combinations is sketchy)
ariaRelevant: "", // 'additions', 'additions removals', 'removals' - does not work with role: alert
message: "" // message to pass to screenreader
};
})(jQuery);