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

Replace Broken Images???

P: n/a
Not sure if this can be done with javascript but I would like to find a way
to detect if an image is missing and replace it with a default image instead
of the dreaded RED X showing up.

So - Is Javascript the way? Or should I look elesewhere?

Thanks in Advance!
Oct 17 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Jake said the following on 10/17/2005 5:11 PM:
Not sure if this can be done with javascript but I would like to find a way
to detect if an image is missing and replace it with a default image instead
of the dreaded RED X showing up.
It can't be done reliably with javascript.
So - Is Javascript the way?
No.
Or should I look elesewhere?


Yes. You shoould look into dependable hosting that will make your images
available. Then you don't have the Red X problem.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 17 '05 #2

P: n/a
VK
> to detect if an image is missing and replace it
with a default image instead of the dreaded
RED X showing up.


<img src="some.gif" onerror="this.src='other.gif';">

Hi there...

Oct 17 '05 #3

P: n/a
VK said the following on 10/17/2005 7:20 PM:
to detect if an image is missing and replace it
with a default image instead of the dreaded
RED X showing up.

<img src="some.gif" onerror="this.src='other.gif';">


And if other.gif is not available?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Oct 17 '05 #4

P: n/a
VK
>>to detect if an image is missing and replace it
with a default image instead of the dreaded
RED X showing up.
<img src="some.gif" onerror="this.src='other.gif';"> And if other.gif is not available?


Then "the dreaded RED X" will still make the trick (I guess it's called
"fall gracefully"?)
The server-side feed is always more reliable than js, but even that
will fail if say your user doesn't have browser, or computer, or
monitor, or nothing at all. You cannot win anyway if someone is trying
DO NOT get your site :-)

Oct 17 '05 #5

P: n/a

Jake <sp******@alltel.net> wrote in message news:88***************************@ALLTEL.NET...
Not sure if this can be done with javascript but I would like to find a way
to detect if an image is missing and replace it with a default image instead
of the dreaded RED X showing up.
Presumably for images on a different server.
So - Is Javascript the way?


It's a trivial matter of using the onload event of each image to build
a table of references to succesfully-loaded images objects.
Then on document load, scan the document.images collection for a
match in the table. If none exists, substitute the .src propery of the
unmatched object with a specified file name.

<script type='text/javascript'>

var imageLog=[];

function replaceImage(substImg)
{
var iLen=document.images.length ,
logLen=imageLog.length ;

for(var i=0; i < iLen; i++)
{
for(var j=0; j<logLen && imageLog[j]!=document.images[i]; j++)
;
if(j==logLen)
document.images[i].src=substImg;
}
}

window.onload=function(){ replaceImage("sorry.jpg"); } // your substitute image here

</script>

In all <img> tags, add: onload='imageLog[imageLog.length]=this'

Mozilla doesn't seem to support onerror, at least for images.

--
Stephen Chalmers http://makeashorterlink.com/?H3E82245A

Oct 18 '05 #6

P: n/a
VK
> Mozilla doesn't seem to support onerror, at least for images.

Of course it does as any other browser since 3rd NN/IE versions.
Of course the domain security applies so you cannot get onerror events
from other domains via frame/iframe tricks. But the same is true for
any other events including onload.

Oct 18 '05 #7

P: n/a

VK wrote:
Mozilla doesn't seem to support onerror, at least for images.


Of course it does as any other browser since 3rd NN/IE versions.
Of course the domain security applies so you cannot get onerror events
from other domains via frame/iframe tricks. But the same is true for
any other events including onload.


Further tests on Mozilla reveal that it appears to be a feature
relating to local files only, unlike I.E, Opera and NN4, which had no
difficulty getting an onerror event locally.
All including Moz got the onerror event for a fictitious file on my
webspace (called from a local file).
I haven't tested what happens between two web domains.

--
S.C.

Oct 18 '05 #8

P: n/a
Well, thanks for the reply but - I am the host - and I consider myself
reliable.

The problem is with clients for whom I have built database driven sites for
and a typo of the filename causes the Red X.

For example, A real estate site, the owner creates a new listing and either
types the image name wrong or fails to upload the image alltogether.
"Randy Webb" <Hi************@aol.com> wrote in message
news:D7******************************@comcast.com. ..
Jake said the following on 10/17/2005 5:11 PM:
Not sure if this can be done with javascript but I would like to find a
way to detect if an image is missing and replace it with a default image
instead of the dreaded RED X showing up.


It can't be done reliably with javascript.
So - Is Javascript the way?


No.
Or should I look elesewhere?


Yes. You shoould look into dependable hosting that will make your images
available. Then you don't have the Red X problem.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

Oct 18 '05 #9

P: n/a
Jake said the following on 10/18/2005 1:22 AM:
Well, thanks for the reply but - I am the host - and I consider myself
reliable.

The problem is with clients for whom I have built database driven sites for
and a typo of the filename causes the Red X.

For example, A real estate site, the owner creates a new listing and either
types the image name wrong or fails to upload the image alltogether.


Then you need to educate your users/client's to test the listing after
updating it and to check the path/capitalization/spelling of the image name.

Short of that, since you are the host, you can setup a server-side
routine to scan the page for img tags, check the existence of the image,
and replace it with an image of your own - all on the server before the
page ever gets to the browser.

The server is the place to fix the problem - not the client.

Signatures can be informative.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Oct 18 '05 #10

P: n/a
VK
> Stephen Chalmers wrote:
Further tests on Mozilla reveal that it appears to be a feature
relating to local files only, unlike I.E, Opera and NN4, which had no
difficulty getting an onerror event locally.
All including Moz got the onerror event for a fictitious file on my
webspace (called from a local file).
Yep, onerror is blocked for localhost for FF (starting at least from
1.0.6) amd Opera (starting at least from 8.01). Some security leak they
had to hach-patch quickly. But it still works as documented from
web-sites, so it's just a debugging inconvenience:

<http://www.geocities.com/schools_ring/onerror_test.html>

P.S. And yes, you better make sure that at least the replacing image
(error.gif in the sample) is here, otherwise you'll get onerror loop
for each missing image. This is what Randy Webb ment to hint up in his
post above. I got it with a big delay, but I still got it :-)

I haven't tested what happens between two web domains.


You may save your time: events are not bubbling through the domain
barrier unless explicetly *sent* by a script on the page of question.

Oct 18 '05 #11

P: n/a
VK said the following on 10/18/2005 12:13 PM:

<snip>
P.S. And yes, you better make sure that at least the replacing image
(error.gif in the sample) is here, otherwise you'll get onerror loop
for each missing image. This is what Randy Webb ment to hint up in his
post above. I got it with a big delay, but I still got it :-)


<grin>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 18 '05 #12

P: n/a


VK <sc**********@yahoo.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...

Yep, onerror is blocked for localhost for FF (starting at least from
1.0.6) amd Opera (starting at least from 8.01). Some security leak they
had to hach-patch quickly. But it still works as documented from
web-sites, so it's just a debugging inconvenience:

<http://www.geocities.com/schools_ring/onerror_test.html>
Err - my Mozilla 1.7.11 isn't loading the oops gif. I.E & Opera 7.54 O.K.
P.S. And yes, you better make sure that at least the replacing image
(error.gif in the sample) is here, otherwise you'll get onerror loop
for each missing image. This is what Randy Webb ment to hint up in his
post above. I got it with a big delay, but I still got it :-)
Methinks that can be circumvented by: onerror='this.onerror=null; this.src="error.gif"'
I haven't tested what happens between two web domains.


You may save your time: events are not bubbling through the domain
barrier unless explicetly *sent* by a script on the page of question.


I thought I'd wasted my time writing that routine, but unless onload has
similar restrictions, it may be more useful than I thought.

--
S.C.

Oct 19 '05 #13

P: n/a
Stephen Chalmers said the following on 10/18/2005 8:33 PM:
VK <sc**********@yahoo.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...

P.S. And yes, you better make sure that at least the replacing image
(error.gif in the sample) is here, otherwise you'll get onerror loop
for each missing image. This is what Randy Webb ment to hint up in his
post above. I got it with a big delay, but I still got it :-)

Me thinks that can be circumvented by: onerror='this.onerror=null; this.src="error.gif"'


But if error.gif is not available for whatever reason, then you still
get the Red X which is the point that he alludes to with my hint.

I *still* think that waiting for the image to be displayed in the
browser to try to replace it is attempting to solve the wrong problem.

If the image is tested before it is ever entered into the database and
the path/etc are verified, then the problem in this thread becomes
non-existent.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 19 '05 #14

P: n/a
Randy Webb <Hi************@aol.com> wrote in message news:KK********************@comcast.com...
Stephen Chalmers said the following on 10/18/2005 8:33 PM:
VK <sc**********@yahoo.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...
But if error.gif is not available for whatever reason, then you still
get the Red X which is the point that he alludes to with my hint.

I *still* think that waiting for the image to be displayed in the
browser to try to replace it is attempting to solve the wrong problem.

If the image is tested before it is ever entered into the database and
the path/etc are verified, then the problem in this thread becomes
non-existent.


My understanding of the problem, subsequently confirmed was that a
means was required to substitute images missing or effectively missing
from an uncontrollable location.
Obviously to be displayable the substitute message must be in place,
but I wanted to show that its absence does not need to cause an
operational error like constant re-triggering of the error event.

--
S.C.

Oct 19 '05 #15

P: n/a
VK
>> <http://www.geocities.com/schools_ring/onerror_test.html>
Err - my Mozilla 1.7.11 isn't loading the oops gif. I.E & Opera 7.54 O.K.


The latest FF from Mozilla is 1.0.7 (minor 1.7.12) and it handles
onerror just fine; either your installation problem or minor 1.7.11 had
a bug in it.

Any way it was good to recall *onerror* event which is often forgotten
despite its greate potential.

The whole issue with missing images may arise on multi-user community
sites (like former geocities.com). Users often do upload files
properly, but later delete some of them forgetting to update their
pages. In such situations JavaScript-onerror feedback may be an option
against server-side page scanning after each update (still would go
with server-side though).

Oct 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.