PDA

View Full Version : Problem with Javascript.



JonathanCrow
01 Oct 2010, 07:39 AM
Okay, i've written a piece of Javascript which does almost exactly what I want, count upwards from a given number by 1 each click, changing the url address for a picture. The idea is to have a working navigation without having to make lots of html pages but instead just use one where you can change the image. However, I want to limit it so that after a certain number of clicks it stops changing, that way an invalid image url doesn't appear.

The current code allows the variable i am using (x) to count infinately.

Also, I am wondeering if this next idea is possible.

Since 'x' is a global variable, if I have it in an external script I was hoping that on other pages, when clicking a link to get to a certain page, lets say page 17, i can set 'x=17' and instead of loading with the default page it would load with page 17 straight away.

I'm sure it's possible but i really lack the skill with javascript to work it out.

My current code is as follows, this isn't the site, but the code i have here will be worked into my site once I have a working script.

I have also been told this codesn't work in Firefox, but I have no idea why.



<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 for me to change the 'Input' into an 'a href', I want to turn it into a rollover image instead.

Sorry if this is a pain in the arse, cheers for any help you can provide.