Hi I'm a student currently doing an html and javascript project. I originally came to this site to look for a way to take an image from a javascript array that's referenced in another array and display it in a pre-defined html table.
As to be expected I never found an exact answer to the problem but managed to work my own way to the solution using bits and pieces of code and knowledge provided from a variety of posts and users answering said posts.
Now that it works I'd like to ask all of you if you could help me to simplify the code or if you could suggest alternate, easier ways of doing this in the future.
I'm a newling to both javascript and html. I'm using Firefox and I've yet to use jquery.
Here's the code from the html page in question.
<body>
<table border="4px">
<caption>
Pet Table
</caption>
<tr>
<!-- Images-->
<td>
<script>
display(bat)
</script>;
</td>
<td>
<script>
display(goat)
</script>
</td>
<td>
<script>
display(butterfly)
</script>
</td>
</tr>
</table>
</body>
Here's the javascript with three examples.
function pet(species, name, colour, size, food, limb, img) {
this.species = species; //property of a pet
this.name = name; //property of a pet
this.colour = colour; //property of a pet
this.size = size; //property of a pet
this.food = food; //property of a pet
this.limb = limb; //property of a pet
this.img = img; //property of a pet
this.move = move; //a function of a pet defined to the pet
}
//array for images used in the pet array
var imgArray = new Array();
imgArray[0] = new Image();
imgArray[0].src = "resources/bat.gif";
imgArray[0].height = "200";
imgArray[0].width = "200";
imgArray[1] = new Image();
imgArray[1].src = "resources/goatVec01.png";
imgArray[1].height = "200";
imgArray[1].width = "200";
imgArray[2] = new Image();
imgArray[2].src = "resources/butterfly.gif";
imgArray[2].height = "200";
imgArray[2].width = "200";
<!-- more images -->
//adding variables to the pet array
var bat = new pet("fruit bat", "bats", "grey", "small", "apples", "wings", "0", move);
var goat = new pet("goat", "bastard", "off white", "goat-sized", "clothing", "hooves", "1", move);
var butterfly = new pet("butterfly", "flutterby", "rainbow", "petite", "nectar", "wings", "2", move);
<!-- more variables -->
//function used to call and display the images
function display(pet) {
var i = document.write(imgArray[pet.img].outerHTML);
}
If I many ask a second question. Since I started using Firefox I've had to use this bit of code to get html to work properly but I'd like to know if there's another method to do this apart from adding these to every page.
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
List of posts that were a great help
Image from an array HTML Javascript
How to display an image thats in an array? JavaScript
How to display image with javascript?
The character encoding of the HTML document was not declared