Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Web Speech API Demo/Example - Search Form Input -
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Web Speech API demo</title>
<link rel="stylesheet" href="css/style.css">
<!--[if IE]>
<script src=""></script>
<body id="home">
<h1>Web Speech API demo - Speak to Search</h1>
<form action="">
<input type="search" id="q" name="q" results="5" autofocus autosave="unique" placeholder="search...">
<input id="talk" type="button" value="record">
<p id="message"></p>
<script type="text/javascript">
window.addEventListener('load', function() {
//set global speech and message variables
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition || null;
var msg = document.getElementById("message");
var q = document.getElementById("q");
//check for speech recognition API support
if (window.SpeechRecognition === null) {
//disable talk+record input
document.getElementById("talk").setAttribute('disabled', 'disabled');
msg.innerHTML = '<strong class="loading">Bummer… your browser does not support speech input. Type in your search and hit enter.</strong>';
} else {
var recognition = new window.SpeechRecognition();
//recognition.continuous = true; // keep processing input until stopped
//recognition.interimResults = true; // show interim results
recognition.lang = 'en-US'; // specify the language
recognition.onresult = function(event) {
if (event.results.length > 0) {
msg.innerHTML = '<strong class="loading">Hold on... running your search.</strong>';
q.value = event.results[0][0].transcript;
//set up event to start recognition when input is clicked/touched
document.getElementById("talk").addEventListener('click', function() {
msg.innerHTML = '<strong class="loading">Talk to me…</strong>';
//set error message if recognition fails
recognition.onerror = function(event) {
msg.innerHTML = '<strong class="loading">Could not hear you. Can you try again?</strong>';
console.log('Recognition error: ' + event.message);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.