I am an intermediate newbie in the world of JavaScript. My question concerns the Try It Out example entitled
Displaying a Random Image when the Page Loads, page 204.
I am not getting any response from the onclick() event that changes images randomly.
While I have modified the original code to include HTML5 rather than XHTML, I doubt this would cause the code not to function correctly. If I am wrong,
mea culpa.
As I say, clicking on the flag images produces absolutely no result.
Anyway, I paste in my code below. Hopefully a more insightful sort than myself will be able to point out my error:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="../../css/exercise.css" />
<style>
img {
border: 0;
}
</style>
<title>Try It Out Exercise 3: Random Image</title>
<script src="../../
js/navLib.
js">
/* Library of Navigational Functions */
</script>
<script>
var myImages = new Array("..\..\images\usa.gif", "..\..\images\canada.gif",
"..\..\images\jamaica.gif", "..\..\images\mexico.gif");
function changeImg(that) {
var newImgNumber = Math.round( Math.random() * 3 );
while ( that.src.indexOf( myImages[newImgNumber] ) != -1) {
newImgNumber = Math.round( Math.random() * 3 );
} // end while
that.src = myImages[newImgNumber];
return false;
} // end changImg(that)
</script>
</head>
<body>
<header>
<hgroup>
<h1>Try It Out Exercise 3</h1>
<h2>Displaying a Random Image When the Page Loads</h2>
</hgroup>
</header>
<section>
<img name="img0" src="../../images/chapter.06/usa.gif"
width="144" height="83"
onclick="return changeImg(this)" />
<img name="img1" src="../../images/chapter.06/mexico.gif"
width="144" height="83"
onclick="return changeImg(this)" />
</section>
<footer>
<input type="button" title="Return to Chapter 6 Index"
value="Return to Chapter 6 Index"
onClick="goToPage('../#tryItOut');" />
</footer>
</body>
</html>
================================================== ===
Many thanks in advance for any assistance you can provide.
Sincerely,
Robert Hieger