473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replace Broken Images???

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
15 6192
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
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
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
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

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
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

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
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
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
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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John at Free Design | last post by:
Thanks in advance for the help and let me know if this should be posted to another MSDN. I am developing a web-based application in VS.NET2003 using VB. When I insert a graphic into a project...
5
by: pembed2003 | last post by:
Hi all, I need to write a function to search and replace part of a char* passed in to the function. I came up with the following: char* search_and_replace(char* source,char search,char*...
7
by: Jacob | last post by:
Has anybody else encountered a problem when running your asp.net applications off your localhost and having broken image links? The weird thing is, the links aren't really broken. The reference...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.