| home / programming / javascript / diaries / 13 |
|
|
Now that we know about the different types of arrays, we'll learn how to manipulate them in order to make them more functional. In this section, we'll look at the properties and methods that are commonly used for most coding situations.
The length property returns the number of elements in an array. The format is arrayName.length. The length property is particularly useful when using a loop to cycle through an array. One example would be an array used to cycle banners:
var bannerImg = new Array();
bannerImg[0]="image-1.gif";
bannerImg[1]="image-2.gif";
bannerImg[2]="image-3.gif";
var newBanner = 0
var totalBan = bannerImg.length
function cycleBan() {
newBanner++
if (newBanner == totalBan) {
newBanner = 0
}
document.banner.src=bannerImg[newBanner]
setTimeout("cycleBan()", 3*1000)
}
window.onload=cycleBan;
This portion is then placed in the body where the banner is to be displayed:
<img src="image-1.gif" name="banner">
Let's take a look and see what happened here:
|
There are a total of five properties for the Array object. In addition to the length property listed above, the others are:
|
The other properties listed here are either more advanced or seldom used. For now, we'll stick to the basics.
| home / programming / javascript / diaries / 13 |
| ||||||||||||||||||||
URL: