user2190672
user2190672

Reputation: 11

How can i call javascript function into src element of Audio tag in HTML5

Following is my code where i have written one function in JavaScript and trying to call it in src element of audio tag. also want to know that whether return "http://translate.google.com/translate_tts?tl=en&q=this is a test" + File;

statement is ok to get google TTS audio file?

<html>
<body>
<script language="javascript" type="text/javascript">
function myFunction()
{
    var File = "this is a test";
    return "http://translate.google.com/translate_tts?tl=en&q=this is a test" + File;
}
</script>
<audio id="speech" src="javascript:myFunction();" controls="controls" autoplay="autoplay"></audio>

Upvotes: 1

Views: 2056

Answers (1)

codebox
codebox

Reputation: 20254

That won't work because the contents of the 'src' tag will not be parsed as JavaScript, it will be treated as an ordinary string.

You could move the script tag below the audio tag and then do this below your definition of 'myFunction':

document.getElementById('speech').src = myFunction();

An even better approach would be to move your JavaScript code into a separate .js file, and set the 'src' attribute in the 'onload' handler for the page, rather than mixing JavaScript source code in with your HTML markup.

Upvotes: 3

Related Questions