Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image swap with testing

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I have a calendar that is used to select a date and then display an image based on that date. Now future dates of the pictures dont exist on the server. I need to test to see if an image exists before displaying it. If the image does not exist I want to display a default image.

Code:
    var imgDefault  = "day-2005-03-03.png";
	var imgSrc      = "day-2005-03-04.png";
	var imgW        = "30";
	var imgH        = "30";
    var imgBaseDir  = "";
    var imgExt      = ".png";
    var imgBaseName = "day-";
	var imgNew      = "";
	var urlBase     = "[URL unfurl="true"]http://localhost/";[/URL]

function dateChanged(calendar) {
    if (calendar.dateClicked) {
      var y = calendar.date.getFullYear();
      var m = calendar.date.getMonth() + 1;     
      var d = calendar.date.getDate();      
		if (m < 10) {
			m = "0" + m;
		}
		if (d < 10) {
			d = "0" + d;
		}

		 imgNew = urlBase + imgBaseDir  + "/" +imgBaseName + y + "-" + m + "-" + d + imgExt;
		 
		 testImage(imgNew);
		
    }
  };

function testImage(URL) { 
   var tester=new Image(); 
   tester.onLoad=switchImage(graph, imgNew ); 
   tester.onError=switchImage(graph, imgDefault); 
   tester.src=URL; 
} 


function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images.graph.src = imgSrc;
    }
  }
}

<img src="day-2005-03-04.png" name="graph">

Any Help would be greatly appreciated, Thank



haunter@battlestrata.com
 
A quick search of the forum shows this post I made a while back:


You might also consider using server-side code to return a default image if none exists to cater for browsers that don't support the image onerror event.

Hope this helps,
Dan


The answers you get are only as good as the information you give!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top