JonathanCrow
01 Oct 2010, 09:00 AM
Hi, I am very new to Javascript. Reasonably proficient with Html and Css however.
I am having trouble implement Java Variables properly.
Okay, i've written a piece of Javascript which does almost exactly what I want. It changes a url to a picture (called '1.png') by editing the url counting upwards by 1 each click (to '2.png). However I have been unable to finish it properly, I want to limit it so that after a certain number of clicks it stops changing, that way an invalid url doesn't appear. So, for instance, if I have 12 pictures, it will cycle a picture per click until it counts to twelve but not '13.png'. Also, my code apparantly does not work in FireFox though I am unsure as to why.
Another thing I am trying to implement 'x' as a global variable, so that if I have it in an external script I was hoping that on other pages that load that external java, when clicking a link to get to a page. such as page 17, i can set 'x=17' and instead of loading with the default image ('1.png) it would load with image 17 ('17.png) straight away.
I'm sure it's possible but i really lack the skill with javascript to work it out.
<html>
<head>
<SCRIPT language="JavaScript">
x=1;
z='.png';
function Next_onclick()
{
document.getElementById("ImageBox").src =(x=++x)+z;
}
</SCRIPT>
</head>
<body>
<Input type="Image" src="submit_out.gif" onclick="return Next_onclick()"></input>
<div id="myDiv">
<img id="ImageBox" src="1.png" />
</div>
</body>
</html>
One final question. Is it possible to change the <input> field into an <a href> field? I want to make the link into a rollover image.
Thank you for any help you can provide.
I am having trouble implement Java Variables properly.
Okay, i've written a piece of Javascript which does almost exactly what I want. It changes a url to a picture (called '1.png') by editing the url counting upwards by 1 each click (to '2.png). However I have been unable to finish it properly, I want to limit it so that after a certain number of clicks it stops changing, that way an invalid url doesn't appear. So, for instance, if I have 12 pictures, it will cycle a picture per click until it counts to twelve but not '13.png'. Also, my code apparantly does not work in FireFox though I am unsure as to why.
Another thing I am trying to implement 'x' as a global variable, so that if I have it in an external script I was hoping that on other pages that load that external java, when clicking a link to get to a page. such as page 17, i can set 'x=17' and instead of loading with the default image ('1.png) it would load with image 17 ('17.png) straight away.
I'm sure it's possible but i really lack the skill with javascript to work it out.
<html>
<head>
<SCRIPT language="JavaScript">
x=1;
z='.png';
function Next_onclick()
{
document.getElementById("ImageBox").src =(x=++x)+z;
}
</SCRIPT>
</head>
<body>
<Input type="Image" src="submit_out.gif" onclick="return Next_onclick()"></input>
<div id="myDiv">
<img id="ImageBox" src="1.png" />
</div>
</body>
</html>
One final question. Is it possible to change the <input> field into an <a href> field? I want to make the link into a rollover image.
Thank you for any help you can provide.