To immediately announce error for control/input I have 2 choices:
alert. [errorMessageHere]
const el = document.createElement('div');
el.setAttribute('role', 'alert');
el.className = 'visually-hidden';
el.textContent = 'Some message here';
document.body.append(el);
.visually-hidden {
position: absolute;
height: 1px;
width: 1px;
top: 0;
left: 0;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
}
window.speechSynthesis.speak
. NVDA announce [errorMessageHere]const speakObj = new SpeechSynthesisUtterance('Some message here');
window.speechSynthesis.speak(speakObj)
What I really notice that in NVDA you can disable text-to-speech and show speech-viewer. First case will be logged into speech-viewer but 2nd case - not. NVDA directly announce text when NVDA is opened even if text-to-speech is disabled.
I'd prefer to use window.speechSynthesis.speak instead of creating extra HTMLElements per each case when some details/errors must be announced but I'm not sure is it correct alternative of role 'alert' since window.speechSynthesis.speak is not logged by NVDA at all (but only announced).
Could help me to understand what the best way to be announced/read by screen reader ?
You should definitely prefer live regions and role alert over speech API.
When using live regions or role alert, the message goes through the screen reader, which brings several important advantages:
None of the three are possible when using speech API:
In fact, use cases for each of the two are quite different: