Skip to content

Commit

Permalink
Merge pull request #5 from Yoast/stories/af/4-dom-ready
Browse files Browse the repository at this point in the history
Create the ARIA live regions when the DOM is ready.
  • Loading branch information
omarreiss authored Jul 27, 2016
2 parents 68f822f + 0102884 commit a7f7902
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 26 additions & 8 deletions a11y-speak.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ var addContainer = function( ariaLive ) {
return container;
};

/**
* Specify a function to execute when the DOM is fully loaded.
*
* @param {Function} callback A function to execute after the DOM is ready.
*/
var domReady = function( callback ) {
if ( document.readyState === "complete" || ( document.readyState !== "loading" && ! document.documentElement.doScroll ) ) {
callback();
} else {
document.addEventListener( "DOMContentLoaded", callback );
}
};

/**
* Create the live regions when the DOM is fully loaded.
*/
domReady( function() {
if ( containerPolite === null ) {
containerPolite = addContainer( "polite" );
}

if ( containerAssertive === null ) {
containerAssertive = addContainer( "assertive" );
}
});

/**
* Clear the live regions.
*/
Expand All @@ -44,14 +70,6 @@ var clear = function() {
* polite or assertive. Default polite.
*/
var A11ySpeak = function( message, ariaLive ) {
if ( containerPolite === null ) {
containerPolite = addContainer( "polite" );
}

if ( containerAssertive === null ) {
containerAssertive = addContainer( "assertive" );
}

// Clear previous messages to allow repeated strings being read out.
clear();

Expand Down
2 changes: 1 addition & 1 deletion a11y-speak.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7f7902

Please sign in to comment.