By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,628 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,628 IT Pros & Developers. It's quick & easy.

Image exists?

P: n/a
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() // public variable
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dennis Allen wrote on 19 mrt 2005 in comp.lang.javascript:
if (document.images) {
var tester=new Image() // public variable
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


Try this:

function good(){
alert('good')
}

function bad(){
alert('bad')
}

var tester=new Image() // public variable
tester.onload=good
tester.onerror=bad

tester.src="http://www.temp.com/image.jpg"

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2

P: n/a
Ah. onclick, onerror, onload can't be a string. Must call the function
directly. Thanks...Dennis

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
Dennis Allen wrote on 19 mrt 2005 in comp.lang.javascript:
if (document.images) {
var tester=new Image() // public variable
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


Try this:

function good(){
alert('good')
}

function bad(){
alert('bad')
}

var tester=new Image() // public variable
tester.onload=good
tester.onerror=bad

tester.src="http://www.temp.com/image.jpg"

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3

P: n/a
Oops. Here's example:

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

This discussion thread is closed

Replies have been disabled for this discussion.