Click to See Complete Forum and Search --> : Java script in <head> not working


sirff
12-31-2003, 02:07 AM
I must admit up front that this is my first experiment in Java. I was looking for a scropt that I could use on my website that would rotate pics (perhaps on refresh, or every 10 seconds or so). I found this script here:
http://www.laughland.biz/javascript.php?scid=3
and placed it within the html of my page like it said (with <head>) but this is what I got:)
http://www.yoursourceinjapan.com/testing.htm
Can someone look at the html of this page and see what I have done wrong?
Many thanks
Kevin

AdamGundry
12-31-2003, 02:13 AM
You need this before the Javascript code starts:

<script type="text/javascript">

and this after it finishes:

</script>

Adam

fredmv
12-31-2003, 02:18 AM
Welcome to the forums.

Just for clarification, you're working with JavaScript — not Java. They're two entirely different languages that have almost nothing in common except their names; also just to note, JavaScript is an interpreted client-side scripting language from Netscape, where Java is an object-oriented complied (and kind of interpreted considerding it runs on a virtual machine) programming language from Sun Microsystems.

Now that we've cleared that up, your problem is quite simple — you just don't have your JavaScript code in-between <script> tags. Therefore, put the following code into your <head> and it should begin to work correctly:<script type="text/javascript">
//<![CDATA[
var images =
[
'image1.gif',
'image2.gif',
'image3.gif
]

function showImg()
{
var number = images.length, increment = Math.floor(Math.random() * number), strTemp ='<img src="' + images[increment] + '">';
document.write(strTemp);
}

var rotate = 5000, count = 0;

function rotateImg(myImage)
{
myImage.src = images[count];
count++;
if(count == images.length) count = 0;
setTimeout
(
function()
{
rotateImg(myImage);
},
rotate
);
}
//]]>
</script>I've also tweaked the script; just couldn't resist. ;)

sirff
01-01-2004, 07:13 AM
Thank you very much Andrew. I have made the tag changes to the script but still cannot get it to work. Can you have a quick look to see what is wrong now?
Again the page is here:
http://www.yoursourceinjapan.com/testing.htm
Thanks and Happy New Year
Kevin

AdamGundry
01-01-2004, 07:23 AM
I suggest you use the code fredmv provided, in particular note that it should be <script type="text/javascript"> not <script type="javascript">. In addition, you have this:

<script language="javascript">ShowImg(images[images/web9.jpg],images[images/web10.jpg],images[images/web11.jpg]);</script>

when it should be this:

<script type="text/javascript">ShowImg();</script>

Adam

P.S. For clarification, there are two commonly used version of the <script> start tag:
<script language="javascript"> - this is deprecated, and should be replaced by
<script type="text/javascript"> - this is correct

sirff
01-02-2004, 10:53 PM
Thanks loads guys. I got further using your advice. I am still stuck on a few things.
I put your code here:http://www.yoursourceinjapan.com/testing2.htm
but I am confused on how to call up the pictures in the body of the page. For example in the old code (in the head) it showed this:
images[0] = "images/web9.jpg";
images[1] = "images/web10.jpg";
images[2] = "images/web11.jpg";
and in yours it replace that with this:
'image1.gif',
'image2.gif',
'image3.gif
I am assuming (no doubt wrongly) that I am to replace image1.gif with web9.jpg (since this is the name of the file I need) and 2 with web10.jpg and so on. I tried this but can't get the pictures to show up at all, let alone rotate.
Also in the code that I actually insert in the body where I want the pics, is it still this? <script language="javascript">ShowImg();</script> and if so then I am assuming I just put the pics I want inside the ( and )?

On the original code - changing the head tags like you suggested, I got a bit further
http://www.yoursourceinjapan.com/testing.htm
However I am stuck on 2 things. 1) How can I place different pics to rotate in each of the spots? For example in the first table I have: ShowImg(0,1) and in the second:ShowImg(1,2) but in each instance they are calling up 1,2, and 3.
2) In the third box I have used the rotate code (since what I ultimately want is for each table to have different pics rotating every 3 to 5 seconds NOT on page reload) <img name="MyImage" src="images/web9.jpg" border="0" > but I am getting the picture to show up but it won't rotate. I have tried putting more pics in that code but cannot get it to work correctly.

I know this is a bit long but I must say that I really do appreciate the help you have given me thus far.
Thanks again
Kevin

fredmv
01-02-2004, 11:03 PM
You're quite welcome Kevin. I'll answer this piece by piece:Orginally posted by sirff
I am assuming (no doubt wrongly) that I am to replace image1.gif with web9.jpg (since this is the name of the file I need) and 2 with web10.jpg and so on.Quite the contrary. You're absolutely correct. The way I provded makes it so you don't need to specify what element in the array you're at (JavaScript will compute it for you!).Orginally posted by sirff
<script language="javascript">ShowImg();</script>
I shouldn't have changed the case of the function name, but it's now showImg instead (note the lowercase "s"). I made this change because it is a better practice to name functions like this (each different word begins with a capital letter and the first starts with a lowercase letter).Orginally posted by sirff
How can I place different pics to rotate in each of the spots? For example in the first table I have: ShowImg(0,1) and in the second:ShowImg(1,2) but in each instance they are calling up 1, and 3.
I'll explain, this is perfectly logical. Arrays start at 0, therefore 1 is the second element in the array, and 2 is the third. Orginally posted by sirff
In the third box I have used the rotate code (since what I ultimately want is for each table to have different pics rotating every 3 to 5 seconds NOT on page reload) <img name="MyImage" src="images/web9.jpg" border="0" > but I am getting the picture to show up but it won't rotate. I have tried putting more pics in that code but cannot get it to work correctly.
I believe the code is currently configured to only work on one image. Although, it could easily be tweaked to support more images.Orginally posted by sirff
I know this is a bit long but I must say that I really do appreciate the help you have given me thus far.No problem. ;)

sirff
01-03-2004, 12:21 AM
On this page http://www.yoursourceinjapan.com/testing.htm I have the pictures showing up and rotating up page reload. However they are still loading all the pics in the array and not just the ones I want. How can I fix this?
On this page (entirely your code) http://www.yoursourceinjapan.com/testing2.htm - I still can't get the pics to even show up. I have tried the following unsuccessfully:
<script language="javascript">showImg(1,2);</script>
<script language="javascript">showImg(images/web9.jpg, images/web10.jpg,);</script>
and
<script language="javascript">showImg(web9.jpg,web10.jpg);</script>
any ideas here?

Thanks agian:)
Kevin