Here are some important things about functions. These were lifted from one of the great books of the Head First Series, Head First JavaScript Programming.
function
keyword, followed by the name of the function.
return
statement.
var
, that variable will be global, which could have unintended consequences in your program.
alert
, prompt
, and Math.random
, to do work in your programs.
In the following there is an example of a webpage that use Javascript functions. Your task is to understand what the code does and then perform certain modifications that will allow you to reuse the code in different situations.
The first example uses a Javascript function to add an audio element on the fly to a page. Press each button to hear the sound. Then look at the source of the page. The relevant part is this:
<div id="words"> <p><span>apple</span><img src="icons/play.png" alt="sound icon"><audio></audio></p> <p><span>pomegranate</span><img src="icons/play.png" alt="sound icon"><audio></audio></p> <p><span>watermelon</span><img src="icons/play.png" alt="sound icon"><audio></audio></p> <p><span>cucumber</span><img src="icons/play.png" alt="sound icon"><audio></audio></p> </div>
As you can notice, the audio
tag is empty, and the sound is added by the Javascript
code whenever the sound icon image that serves as a button is clicked.
Here is the JavaScript code that automatically adds the event handler playSound
to every img
element. This is very similar to what we did in the
Animal Gallery example.
// get the list of img elements that are sound icons var iconsList = document.querySelectorAll("#words img"); // iterate through the list to bind to every element the event handler // for the onclick event for (var i=0; i < iconsList.length; i++){ var icon = iconsList[i]; icon.onclick = playSound; }
Notice the use of the document.querySelectorAll
. Differently from
document.querySelector
which returns only the first element it finds
the first method returns all elements defined by the selector as an array
of elements. Try it out in the console to see it for yourselves.
Below is the code for the playSound
function. Notice that it uses
the special variable this
to refer to the clicked icon. Then it uses
the DOM structure (the fact that the element img
has two siblings, one
on each side) to access them and get the word from the previous sibling and set the audio
attributes to the next sibling.
function playSound(){ console.log(this) // Check that it refers to the <img> element //get the text from the <span> element that is its previous sibling //Check the HTML code to see that <img> and <span> are siblings. var word = this.previousSibling.innerHTML; // get the <audio> element which is its next sibling var audio = this.nextSibling; // specify values for the attributes of the <audio> element // notice how the URL is generated by concatenating a fixed part // with the concrete word var attributes = {src: "https://ssl.gstatic.com/dictionary/static/sounds/de/0/" + word + ".mp3", autoplay: "autoplay", type: "audio/mpeg"}; // set the attributes of the <audio> element, so that it can play the sound for (property in attributes) { audio.setAttribute(property, attributes[property]); } }
Finally, notice the special use of the for
loop with the properties of an object.
We are using the for loop, so that we don't need to call the method setAttribute
many times for each attribute we want to set.
Now that you know how the example works, modify the code for your purposes. Here are some ideas, but feel free to pursue your own ideas:
playSound
function to read the word from the input and then pronounce it.
Note: not all English words are in the dictionary. You can check the console to see for words
that were not found, you'll see a GET error.playSound
method so that now it works
for both pages. For that, you'll need to use parameters for the input word and the audio element,
so that the function is independent of the HTML elements of the page. Then, invoke
your function with arguments, depending on which page is using it.