Skip to content

Instantly share code, notes, and snippets.

@fwaechter
Last active September 27, 2017 18:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwaechter/d5106455b3302b2aca55fe82de01b9ea to your computer and use it in GitHub Desktop.
Save fwaechter/d5106455b3302b2aca55fe82de01b9ea to your computer and use it in GitHub Desktop.
Simple Text2Speech Snippet in 10 Lines of JavaScript using URLSearchParams (not Supported in Internet Explorer): https://developer.mozilla.org/de/docs/Web/API/URLSearchParams
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Text2Speech</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<script type="text/javascript">
//<![CDATA[
var searchParams = new URLSearchParams(window.location.search);
var text2peech = searchParams.get("q");
var say = function(text) {
window.speechSynthesis.cancel();
return window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
};
say(text2peech);
document.write(text2peech);
//]]>
</script>
<form action="say.html" role="form" method="get">
<input type="text" name="q" />
<input type="submit" value="Sprich!" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment