Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join Zeta Resources, you'll be able to access member-only sections, and use many member-only features such as requesting free skins, for both Zeta Boards and Invisionfree. It doesn't end there, we also offer free Graphics, Codes and even Affiliation! Registration is simple, fast, and completely free.


Join Zeta Resources!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Create a Random Affiliate; Javascript
Topic Started: Nov 28 2007, 09:24 PM (141 Views)
Viral
Member Avatar

Just bear with me, this is from scratch, and so is my JavaScript ability.


Preview: http://z3.invisionfree.com/keir/index.php?act=idx


Step 1

Quote:
 

<script type="text/javascript">
<!--

//-->
</script>


Start the Javascript with the basics, defining it as Javascript, ending the script and those types of comment tags will make the code act like a comment if not used properly.


Step 2

Quote:
 

<script type="text/javascript">
<!--
var theImages = new Array()
theImages[0] = "Image Link1"
theImages[1] = "Image Link2"

var theLinks = new Array()
theLinks[0] = "URL 1"
theLinks[1] = "URL 2"
//-->
</script>


Created the arrays in this step (theImages & theLinks).


Step 3

Quote:
 

<script type="text/javascript">
<!--
var theImages = new Array()
theImages[0] = "Image Link1"
theImages[1] = "Image Link2"

var theLinks = new Array()
theLinks[0] = "URL 1"
theLinks[1] = "URL 2"

x= Math.floor(Math.random()*theImages.(length-1));
//-->
</script>


Made a random number between 0 and the amount of Images there are. I could have done this for the amount of links as well, but they are both the same. Math.floor() rounds the number down to the nearest integer.



Step 4


Quote:
 

<script type="text/javascript">
<!--
var theImages = new Array()
theImages[0] = "Image Link1"
theImages[1] = "Image Link2"

var theLinks = new Array()
theLinks[0] = "URL 1"
theLinks[1] = "URL 2"

x= Math.floor(Math.random()*theImages.(length-1));

document.write("<a href=\"'+theLinks[x]+'\"><img src=\"'+theImages[x]+'\"></a>");
//-->
</script>


That will display the usual HTML <a href... tag with an Img (<img src..).

Finished!! Thats the code done.


Now to define what each thing does:

Code:
 
<!-- Code Here //-->


If code is not supported by Browser, or has an error, the text will not display.



Code:
 
var theImages = new Array()


var theImages sets a variable named theImages.
new Array() makes that variable an Array which can be used for lists like shown below.

Code:
 
theImages[0] = "Hello.gif"


The [0] shows that it is the first variable in the array theImages. Each array number starts at 0, and progresses by 1 for each variable.




Code:
 
x= Math.floor(Math.random()*theImages.(length-1))


Code:
 
x=
That defines a variable called x, notice I didn't use 'var' this time. Using var is a good habit, but not a necessity. Everything after it will be stored in the variable 'x'.

Code:
 
Math.floor()
Uses the floor function. Anything inside the brackets will be rounded down to the nearest integer, after calculation. Take not of the capital M in Math.

Code:
 
Math.random()
This will be floored with the Math.floor function that we defined earlier, after calcultion. This make a random number between 0 and 1, which isn't very useful. So we add a:

Code:
 
*theImages.(length-1)


That will multiply the 1 with the amount of images defined earlier. This means that instead of getting a random number between 0 and 1, it will be 1 and 2, in our case. But, because the .length was subtracted by 1, the number will be between 0 and 1, the same numbers we have as our variables in the Arrays, 'theImages' and 'theLinks'. The random number then gets floored. This means the number will either be 0 or 1, nothing less or higher.

If you understood any of that, well done.




Code:
 
document.write("<a href=\"'+theLinks[x]+'\"><img src=\"'+theImages[x]+'\"></a>");


document.write shows that they are using the whole document, and writing in it. You start it with a bracket and end with a bracket. You will always have double quotes after the first bracket, and before the last, unless you are using variables.

We start with the double quotes, to start our HTML tag (which I will not explain). Although, you may be wondering why we have backslashes before the double quotes.. They are just so the browser omits them as ending the document.write, instead, starting and ending the HTML.

We close the double quotes and add a '+' sign, then our variable. The + defines a variable coming next, which cant be in a string ( " " ). Then, you add another + to say that you will be carrying on the string (the part with the <img src=""etc.. ).

You may notice after the variable name (theImages and theLinks) that instead of the usual [0] or [1], there is a [x]. 'x' is the random number we defined earlier which will either be 0 or 1, so it is a complete surprise.


Thats it for the Random Affiliate tutorial by Viral/Viralize.


This is copyright to http://s1.zetaboards.com/ZetaResources and cannot be redistributed without the authors permission.



Sorry I blabbed on, I'm tired and yeah you know..
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Coding Tutorials · Next Topic »
Add Reply