WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Javascript Array on Thu Apr 12, 2012 12:12 am

Mr.Joker

avatar
Mr. WebArtz
Mr. WebArtz
Javascript Array
Working with arrays in JavaScript


Hello members of WebArtz, I am Mr.Joker and today I will try to teach you what is an Array. Well, as a name say allready an Array can store some values inside it. For example an array in real life example is :
-Computer, Mouse, Desk , etc.
So thats an Array. But what for we need an Array while coding. Well here is one example of Array:

Code:
<script type="text/javascript">
var movies = new Array(3);
movies[0] = "Titanic";
movies[1] = "V for Vendetta";
movies[2] = "Matrix 1";
for (i=0;i<3;i++)
document.write(movies[i] + "<br />")
</script>

Ok, lets explain a code. We have a variable movies but a value of that variable is a new Array. So I will add several values to single variables. Yes, it is possible because of arrays. In next line i write variable and inside this brackets i add a number 0 which mean that variable is the first one. Computer always count from 0 as you know allready. And now i add some value to that variable. In next row I add number 1 inside brackets which is a new value for variable movies. Now simple add another movie. And do the same for last movie Matrix. Now, If i want to write all 3 movies at once on the screen i can do that by writing document.write(movie[0] + "<br />") and 2 more times that for each movie but its wasting of time. I just simple use for loop and say that there is variable i with value zero. There is condition that say if variable i is less then 3 increase that variable value by the time till it become 3. Why 3? Its because i have 3 movies inside Array. Now i said while that loop exists i want to write document.write(movies[i] + break)... Why like this? Well as you know when variable i have starting value 0 it start to increase. I used that inside movies because i dont want to waste my time to write numbers each time. A computer will do that for me. Its so simple to understand how Array works. As i promesd I will explan you code i gived you in last tutorial which is this one:

Code:
<html>
<head>
<script type="text/javascript">
Rollimage = new Array()
Rollimage[0] = new Image()
Rollimage[0].src = "http://img163.imageshack.us/img163/2686/test1r.png"
Rollimage[1] = new Image()
Rollimage[1].src = "http://img196.imageshack.us/img196/7528/test2zx.png"
function over() {
document.image.src = Rollimage[1].src;
return true;
}
function back() {
document.image.src = Rollimage[0].src;
return true;
}
</script>
</head>
<body>
<a href="www.google.com" onMouseover="over()" onMouseout="back()">
<img src="http://img163.imageshack.us/img163/2686/test1r.png" name="image" border="0"></img></a>
</body>
</html>

Lets start. I declare a variable Rollimage and set it to new Array. When i write Rollimage[0] = new Image(); i say now that that variable have a value of image. And then in next row I use again same variable but with .src because i want to set a URL of image. Then i create another variable with value of image and set some URL for it again. Then i write a function called over because I want to declare what will happend if I use mouse over image. There I say:
Code:
document.image.src = Rollimage[1].src;
return true;
Its simple for me to understand but for you its hard because I still didnt exaplined how to access to elements of forms. Just understand it like this. In image tag i named it image and thats why i say document.image, if I named it something_else i would say document.something_else. src I add because I want some image be screen but which one? Of course it depend which one you want, and i Used the other color which is second variable. Then i do the another function wich will return a begining image back after we use mouse out event. Its really simple and I really have nothing else to add here. If you are still confused what I did you can just ask me in thread.

Notice : These tutorials are copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: Javascript Array on Thu Apr 12, 2012 8:53 am

ankillien

avatar
Administrator
Administrator
Tutorial Accepted Very Happy

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum