Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 2nd, 2010, 02:55 PM
Registered User
 
Join Date: Mar 2010
Location: Tampa, FL
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default How to recycle an image?

In their fine book, Beginning JavaScript v4, in Chapter 10, the authors show how to go through three images. How do I close this and recycle to the beginning?
Code:
<script language=JavaScript type="text/javascript">
var currentImgNumber = 1;
var numberOfImages = 3;

function window_onload()
{
	setTimeout("switchImage()",3000);
	
}
function switchImage()
{
	currentImgNumber++;
	document.imgAdvert.src = 'AdvertImage' + currentImgNumber + '.jpg';
	if(currentImgNumber<numberOfImages)
	{
		setTimeout("switchImage()",3000);   }
		history.go(-2)
		
}
</script>

</head>

<body onload="window_onload()">
<img src="AdvertImage1.jpg" name="imgAdvert" />


</body>
</html>
The Following User Says Thank You to JohnAlex1101 For This Useful Post:
  #2 (permalink)  
Old March 3rd, 2010, 04:09 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Code:
function switchImage()
{
     currentImgNumber++;
     if ( currentImgNumber > numberOfImages ) currentImgNumber = 1;
     document.imgAdvert.src = 'AdvertImage' + currentImgNumber + '.jpg';
     setTimeout("switchImage()",3000);   }
     history.go(-2)
 }
Not sure what the history.go(-2) is for, but the rest is simple enough.

EDIT: I 100% agree w/ PeterPeiGuo. Get rid of that history.go line!

Last edited by Old Pedant; March 4th, 2010 at 03:06 PM..
  #3 (permalink)  
Old March 4th, 2010, 12:15 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

I guess that history.go(-2) was an attempt to go back to image 1 when it is on image 3 (1 - 3 = -2).

However that won't work , as the page never went forwardly or backwardly. Although the image changed, it was always the "same page".

use the following code, and follow the test sequence specified below, you will see what history.go(-2) actually does.
Code:
function switchImage()
{
	if(currentImgNumber < numberOfImages)
	{
		document.imgAdvert.src = ++ currentImgNumber + '.jpeg';
	} else if (currentImgNumber == numberOfImages) {
		history.go(-2);
	} 
	setTimeout("switchImage()",3000);   
}
Steps for testing:
  1. visit www.cnn.com (this is page 1)
  2. visit www.abcnews.com (this is page 2)
  3. visit the testing page (this is page 3, and remains as page 3 while the image changes)
  4. Wait until you see the last page, wait for 3 more seconds, you will be led back to www.cnn.com

Last edited by PeterPeiGuo; March 4th, 2010 at 12:30 AM..
  #4 (permalink)  
Old March 4th, 2010, 12:54 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

As for the solution that Old Pedant provided, that line with history.go(-2) should be removed. The code won't work unless special condition is met.

If there is no page -2 to go (for example you view the testing page using a brand new tab), your code will work as history.go(-2) takes no effect in this case. Otherwise you will be led away from the testing page upon the first execution of the script.
  #5 (permalink)  
Old March 5th, 2010, 12:01 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

JQuery Cycle plugin is an interesting alternative for this. It gives you many built-in visual effects.

Last edited by PeterPeiGuo; March 5th, 2010 at 03:00 AM..


Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
How to: Upload image to server and save image name in db with other form info Clint PHP How-To 1 October 26th, 2009 05:50 AM
set image on <asp:Image> stored in DataBase myself.panku ASP.NET 2.0 Professional 1 August 11th, 2008 10:41 AM
create recycle bin abaso.jadhav01 ASP.NET 2.0 Basics 3 December 11th, 2006 04:51 PM
Restore file from Recycle Bin eelisMX Pro VB.NET 2002/2003 0 December 8th, 2006 09:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.