switching photos

P

pierre

Hi,
Since saturday I'm trying to find a bug.
In the script below onClick="switch_photos('first')" and
onClick="switch_photos('last')" work fine.
However onClick="switch_photos('prev') and
onClick="switch_photos('next')" don't (no pictures)
Would anybody be so kind to to help me finding this bug?
Thanks for your kind help
Pierre
------------------------------------------------------------------
<SCRIPT>
function switch_photos(param){
var current_image;
var prev_image=-1;
var next_image=-1;
image_id=document.getElementById("sImg");
if (images_list.length>0)
{
for (i=0;i<images_list.length;i++)
{
if (image_id.src==images_list)
{
current_image=i;
if (i!=1)//if the first image=>there
is no previous
prev_image=i-1;
else
prev_image=images_list.length-1;
if (i!=(images_list.length-1))//if
last image=>there is no next
next_image=i+1;
else
next_image=1;
}
}
}
if(param=="prev")
image_id.src=images_list[prev_image];
if(param=="next")
image_id.src=images_list[next_image];
if(param=="first")
image_id.src=images_list[1];
if(param=="last")
image_id.src=images_list[((images_list.length)-1)];
}
var images_list = new Array();
images_list[1]="da1.jpg";
images_list[2]="da2.jpg";
images_list[3]="da3.jpg";
images_list[4]="da4.jpg";
images_list[5]="da5.jpg";
images_list[6]="da6.jpg";
images_list[7]="da7.jpg";
images_list[8]="da8.jpg";
images_list[9]="da9.jpg";
images_list[10]="da10.jpg";
images_list[11]="da11.jpg";
function resizeWindow() {
img = document.getElementById("sImg");
if (img != null) {
if (document.all)
{w = img.width + 30; h = img.height + 110; }
else
{w = img.width + 20; h = img.height + 80; }
window.resizeTo(w, h);
}
}
</SCRIPT>
 
B

Bart Van der Donck

(e-mail address removed) wrote:

Since saturday I'm trying to find a bug.
In the script below onClick="switch_photos('first')" and
onClick="switch_photos('last')" work fine.
However onClick="switch_photos('prev') and
onClick="switch_photos('next')" don't (no pictures)
Would anybody be so kind to to help me finding this bug?
Thanks for your kind help
Pierre
------------------------------------------------------------------
<SCRIPT>
function switch_photos(param){
var current_image;
var prev_image=-1;
var next_image=-1;
image_id=document.getElementById("sImg");
if (images_list.length>0)
{
for (i=0;i<images_list.length;i++)
{
if (image_id.src==images_list)
{
current_image=i;
if (i!=1)//if the first image=>there
is no previous
prev_image=i-1;
else
prev_image=images_list.length-1;
if (i!=(images_list.length-1))//if
last image=>there is no next
next_image=i+1;
else
next_image=1;
}
}
}
if(param=="prev")
image_id.src=images_list[prev_image];
if(param=="next")
image_id.src=images_list[next_image];
if(param=="first")
image_id.src=images_list[1];
if(param=="last")
image_id.src=images_list[((images_list.length)-1)];}

var images_list = new Array();
images_list[1]="da1.jpg";
images_list[2]="da2.jpg";
images_list[3]="da3.jpg";
images_list[4]="da4.jpg";
images_list[5]="da5.jpg";
images_list[6]="da6.jpg";
images_list[7]="da7.jpg";
images_list[8]="da8.jpg";
images_list[9]="da9.jpg";
images_list[10]="da10.jpg";
images_list[11]="da11.jpg";
function resizeWindow() {
img = document.getElementById("sImg");
if (img != null) {
if (document.all)
{w = img.width + 30; h = img.height + 110; }
else
{w = img.width + 20; h = img.height + 80; }
window.resizeTo(w, h);
}}

</SCRIPT>


<!doctype HTML public "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>My web page</title>
<script type="text/javascript">
var pics = ['da0.jpg','da1.jpg','da2.jpg','da3.jpg'];
var c = 0;
function s(p) {
c = p;
if (c == -1) c = pics.length-1;
if (c >= pics.length) c = 0;
document.getElementById('sImg').src = pics[c];
}
</script>
</head>
<body>
<form action="#">
<p><img id="sImg" width="100" height="100" src="da0.jpg" alt=""><br>
<input type="button" value="&lt;&lt;" onClick="s(0)">
<input type="button" value="&lt;" onClick="s(c-1)">
<input type="button" value="&gt;" onClick="s(c+1)">
<input type="button" value="&gt;&gt;" onClick="s(pics.length-1)">
</p>
</form>
</body>
</html>

Hope this helps,
 
H

Henry

Hi,
Since saturday I'm trying to find a bug.
In the script below onClick="switch_photos('first')" and
onClick="switch_photos('last')" work fine.
However onClick="switch_photos('prev') and
onClick="switch_photos('next')" don't (no pictures)
Would anybody be so kind to to help me finding this bug?
Thanks for your kind help
Pierre
------------------------------------------------------------------
<SCRIPT>
function switch_photos(param){
var current_image;
var prev_image=-1;
var next_image=-1;
image_id=document.getElementById("sImg");
if (images_list.length>0)
{
for (i=0;i<images_list.length;i++)
{
if (image_id.src==images_list)


If you read the - src - property of an IMG element you get the
absolute URL not any relative URL assigned. The - images_list - only
contains relative URLs so they will never equal the value retrieved
from - src - and so this - if - block will never be executed.

if(image_id.src.indexOf(images_list) != -1){ ...

- would be a better test, assuming the image name could not be a
substring of any other part of the absolute URL.
{
current_image=i;
if (i!=1)//if the first image=>there
<snip>

If the code ever had got to this point it would not make sense to be
assuming that 1 was the index of the first item in the array. Zero
would be a much more reasonable value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top