Javascript – Random Sounds on Button Press
-
Hi there!
So I’ve finally gotten the following bit of Javascript to work on a regular webpage:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script> function playSounds() { var Sounds = new Array( "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh1.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh2.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh3.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh4.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh5.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh6.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh7.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh8.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh9.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh10.wav", "https://thenumberswhisperer.com/wp-content/uploads/2012/09/laugh11.wav" ); var index = Math.floor(Math.random() * (sounds.length)); $("#element").html("<embed src=\"" + sounds[index] + "\" hidden=\"true\" autostart=\"true\" />"); } </script> </head> <body onload="javascript:playSounds()"> <button onclick="playSounds()"> LAUGH</button> <div id="element"> </div> </body> </html>
But I’m having trouble moving it to my WordPress site. I’ve tucked everything from the <head> section into the header.php between the <head> tags and slipped the “onload=javascript:PlaySounds()” into the body tag after the PHP call for a class so it reads:
<body onload="javascript:playSounds()" <?php body_class(); ?>>
And then put the <button> tag down in the body. The button is generating, but it does NOTHING when I click on it. I just KNOW I’m missing something painfully obvious on this one and I have no idea what it is!
PS – I don’t know javascript. I have a book on javascript and used that to put the script together along with research on the internet. <sigh>
Thanks so much for any help!
Linkie: https://www.faenathara.com/crowsong (it’s in the sidebar)
- The topic ‘Javascript – Random Sounds on Button Press’ is closed to new replies.