Jean Pierre Daviau wrote:
Ha, ha!
And the question is . . .
the [i] does not seem to be red as a number.
alert(totalWidth) gives 0.
We will now consult the oracle, hoping to divine your question.
After all it is signed
X treme newbe
Thank you for reading.
Now there's a problème !
Always post as much of what you're doing as seems reasonable. Are you
running this script in the <head>er of the document? Source files are
read from beginning-to-end (top to bottom as they appear in your
editor). Can't rummage through a collection (document.images) that
hasn't been made yet. Always hook these up to an onload handler:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
function getImageDims()
{
var totalWidth = totalHeight = 0;
for (var i = 0; i < document.images.length; i++)
{
totalWidth += document.images[i].width;
totalHeight += document.images[i].height;
}
alert('total width: ' + totalWidth + '\ntotal height: ' +
totalHeight);
}
window.onload = getImageDims; //no ()!
</script>
</head>
<body>
<img src="http://www.4girls.gov/nutrition/apple-small.jpg">
<img src="http://www.chrisgilman.com/APPLE_small1.jpg">
<img src="http://allthatchocolate.com/images/products/apple_small.jpg">
</script>
</body>
</html>
Image objects have their own onload handlers (like the window, above -
so:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var totalWidth = totalHeight = 0;
function getImageDims()
{
alert('total width: ' + totalWidth + '\ntotal height: ' +
totalHeight);
}
window.onload = getImageDims;
</script>
</head>
<body>
<img src="http://www.4girls.gov/nutrition/apple-small.jpg"
onload="totalWidth+=this.width;totalHeight+=this.h eight">
<img src="http://www.chrisgilman.com/APPLE_small1.jpg"
onload="totalWidth+=this.width;totalHeight+=this.h eight">
<img src="http://allthatchocolate.com/images/products/apple_small.jpg"
onload="totalWidth+=this.width;totalHeight+=this.h eight">
</script>
</body>
</html>