Skip to content

Instantly share code, notes, and snippets.

@redconfetti
Created June 11, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redconfetti/e00fa0a356f16f4252d8 to your computer and use it in GitHub Desktop.
Save redconfetti/e00fa0a356f16f4252d8 to your computer and use it in GitHub Desktop.
Screen Reader Announcer
/**
* Inserts hidden announcement into page for screen readers
* Expects 'cc-visuallyhidden' class to hide element from visual users
**/
var screenReaderAnnounce = function(message) {
// remove existing announcer
var existingAnnouncer = $window.document.getElementById('cc-sr-announcer');
if (document.contains(existingAnnouncer)) {
existingAnnouncer.parentNode.removeChild(existingAnnouncer);
}
// add new announcer
var announcer = $window.document.createElement('div');
var announcerAlert = $window.document.createElement('p');
var announcerHeader = $window.document.createElement('h1');
announcer.setAttribute('id', 'cc-sr-announcer');
announcer.setAttribute('class', 'cc-visuallyhidden');
announcerHeader.innerHTML = 'Last Page Update';
announcerAlert.setAttribute('role', 'alert');
announcerAlert.innerHTML = message;
announcer.appendChild(announcerHeader);
announcer.appendChild(announcerAlert);
$window.document.body.appendChild(announcer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment