Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1327 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Javascript :

loop problem with if inside

Thread title: loop problem with if inside
Closed Thread    
    Thread tools Search this thread Display Modes  
11-14-2007, 12:35 PM
#1
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old  loop problem with if inside

Well my javascript function works fine until image[09] but when greater then 10 it doesn't work anymore. Any suggestions?

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript">
var MaxThumb = 15;

var images = Array();

function preLoad() {
	var i;
	for(i = 0; i < MaxThumb; i++) {
		var image, imageI, bigImage;
		imageI = i + 1;
		
		if(i < 10) {
			image = document.getElementById("image[0"+ imageI +"]");
		}else {
			image = document.getElementById("image["+ imageI +"]");
		}
		
		if(i < 10) {
			bigImage = image.src.substring(0, image.src.indexOf("_")-1);
		}else {
			bigImage = image.src.substring(0, image.src.indexOf("_")-2);
		}
				
		bigImage += (i+1) +".jpg";
		
		images[i] = new Image
		images[i] = bigImage;
	}
}
function show_image(num) {
	document.getElementById("FullSize").src = images[num];
}
</script>

<style type="text/css">
a {
	border: 0;
	text-decoration: none;
}
img {
	border: 0;
}
</style>
</head>

<body onLoad="preLoad()">
<table width="730" cellpadding="0" cellspacing="5">
	<tr>
		<td><a href="#" onclick="show_image(0)"><img src="image01_thumb.jpg" width="100" height="100" id="image[01]" /></a></td>
		<td><a href="#" onClick="show_image(1)"><img src="image02_thumb.jpg" width="100" height="100" id="image[02]" /></a></td>
		<td><a href="#" onClick="show_image(2)"><img src="image03_thumb.jpg" width="100" height="100" id="image[03]" /></a></td>
		<td><a href="#" onClick="show_image(3)"><img src="image04_thumb.jpg" width="100" height="100" id="image[04]" /></a></td>
		<td width="300" rowspan="3"><img src="#" name="FullSize" width="300" height="300" id="FullSize" /></td>
	</tr>
	<tr>
		<td><a href="#" onClick="show_image(4)"><img src="image05_thumb.jpg" width="100" height="100" id="image[05]" /></a></td>
		<td><a href="#" onClick="show_image(5)"><img src="image06_thumb.jpg" width="100" height="100" id="image[06]" /></a></td>
		<td><a href="#" onClick="show_image(6)"><img src="image07_thumb.jpg" width="100" height="100" id="image[07]" /></a></td>
		<td><a href="#" onClick="show_image(7)"><img src="image08_thumb.jpg" width="100" height="100" id="image[08]" /></a></td>
	</tr>
	<tr>
		<td><a href="#" onClick="show_image(8)"><img src="image09_thumb.jpg" width="100" height="100" id="image[09]" /></a></td>
		<td><a href="#" onClick="show_image(9)"><img src="image10_thumb.jpg" width="100" height="100" id="image[10]" /></a></td>
		<td><a href="#" onClick="show_image(10)"><img src="image11_thumb.jpg" width="100" height="100" id="image[11]" /></a></td>
		<td><a href="#" onClick="show_image(11)"><img src="image12_thumb.jpg" width="100" height="100" id="image[12]" /></a></td>
	</tr>
</table>
</body>
</html>

11-14-2007, 01:09 PM
#2
LeeP is offline LeeP
Status: Member
Join date: Jan 2007
Location: West Midlands, UK.
Expertise:
Software:
 
Posts: 371
iTrader: 5 / 100%
 

LeeP is on a distinguished road

Send a message via MSN to LeeP

  Old

You've got if(i < 10), meaning that it will only go to 9.

Should be if(i <= 10) I think, because It will still work when It gets to 10.

11-14-2007, 01:40 PM
#3
CreativeLogic is offline CreativeLogic
CreativeLogic's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,078
iTrader: 6 / 100%
 

CreativeLogic is on a distinguished road

Send a message via MSN to CreativeLogic

  Old

This is the incorrect code:
Code:
if(i < 10) {
	bigImage = image.src.substring(0, image.src.indexOf("_")-1);
} else {
	bigImage = image.src.substring(0, image.src.indexOf("_")-2);
}
It should simply just be:
Code:
bigImage = image.src.substring(0, image.src.indexOf("_")-1);

11-14-2007, 04:56 PM
#4
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old

but the images are with 01 so when it's below 10 it need just a number because in the name the 0 is already defined and when it's bigger you have to remove the 0 so -2
if you know what i mean

[edit]
Fixed it
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript">
var W3CDOM = (document.createElement && document.getElementsByTagName);

var image = Array();

window.onload = main;

function main() {
	if (!W3CDOM) return;
	
	var img = document.getElementById('images');
	var imgs= img.getElementsByTagName('img');
	
	for(var i = 0; i < imgs.length; i++) {
		
		var bigImage
		
		if (i < 9) {		
			bigImage = imgs.item(i).src.substring(0, imgs.item(i).src.indexOf("_")-1);
		}
		if(i >= 9) {
			bigImage = imgs.item(i).src.substring(0, imgs.item(i).src.indexOf("_")-2);
		}
					
		bigImage += (1+i) +".jpg";

			
		image[i] = new Image
		image[i] = bigImage;
		
		
	}
}

function show_image(num) {
	document.getElementById("FullSize").src = image[num];
}
</script>

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed