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.
Any Help would be greatly appreciated, Thank
haunter@battlestrata.com
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