...

View Full Version : Image exists?



dallen24
03-19-2005, 05:05 AM
Hi. I've been trying to write Javascript code that checking if an image exists. If it doesn't exist, I want to load a known image. I've tried:

if (document.images) {
var tester=new Image();
tester.onload="cImg('good')"
tester.onerror="cImg('bad')"
tester.src="http://www.temp.com/image.jpg"
}

function cImg(flg) {
if (flg == "good") {
document.images.main.alt = "Image is good"
document.images.main.src = "http://www.temp.com/image.jpg"
} else {
document.images.main.alt = "Known image"
document.images.main.src = "image.jpg"
}
}

Problem. As far as I can see, the onload and onerror don't fire. Whether the image exists or not. At least in my IE6. What's the problem? I've tried document.images.main.onload and onerror directly, nothing. Any help is appreciated...Dennis

vwphillips
03-19-2005, 07:41 AM
see

http://www.vicsjavascripts.org.uk/MissingImage/MissingImage.htm

dallen24
03-19-2005, 09:40 PM
Solved the problem. onclick, onload, onerror cannot be strings. Example:

var tester=new Image()
tester.onload=function() {cImg('good')}
tester.onerror=function() {cImg('bad')}
tester.src="http://www.temp.com/image.jpg"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum