473,398 Members | 2,403 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,398 software developers and data experts.

Alternative Image

joe
Hi. Is there a way with Javascript to use a generic image if the one I want
is not found?
I'd appreciate if someone can post a code sample, or poit me to one. Thanks
in advance.
Jul 23 '05 #1
5 4092
joe wrote:
Hi. Is there a way with Javascript to use a generic image if the one I want is not found?
I'd appreciate if someone can post a code sample, or poit me to one. Thanks in advance.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

img {display: block;}

</style>
</head>
<body>
<img
src="http://www.google.com/intl/en/images/logo.gif"
onerror="this.src='http://www.sonofsofaman.com/images/photo-not-available-180x240.gif'">
<img
src="http://www.google.com/intl/en/images/logo.giff"
onerror="this.src='http://www.sonofsofaman.com/images/photo-not-available-180x240.gif'">
</body>
</html>

Could do this more systematically...

Jul 23 '05 #2
joe
Ok. I think I found a solution:

<img src="orig_image.jpg" onerror="this.src='alt_image.jpg';">

Jul 23 '05 #3
joe wrote:
Ok. I think I found a solution:

<img src="orig_image.jpg" onerror="this.src='alt_image.jpg';">


This is not a standards compliant solution, though. The `img' element
does not have an `onerror' attribute in Valid (X)HTML markup, and the
`alt' attribute is required. A standards compliant solution, taking
into account additional DOM features, could be

<!-- Transitional HTML or XHTML DOCTYPE, else use `id' instead of `name' -->
<html>
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function replaceOnError(img, alt_src)
{
if (typeof img.onerror != "undefined")
{
img.onerror = function()
{
this.src = alt_src;
return true;
}
}
}
</script>
...
</head>

<body onload="replaceOnError(document.images['foo'], 'alt_image.jpg');">
...
<img src="orig_image.jpg" alt="foo" name="foo">
...
</body>
</html>

But you rather should consider the ErrorDocument directive of httpd
compatible Web servers (like the Apache HTTP Server) which will also
work without client-side script support.
PointedEars
Jul 23 '05 #4
What garble are you talking about ?

Image loads or not and will not error otherwise.

I you need to event monitor higher stuff,
it may be too late.
What error? Do your local newspaper's photos
make noises when you pull a copy form the vending box.

Which one you thought ?

How is your noisy cartoon and TV replacement of a childhood
and education playing out after all ?
"Alternative Image"

There is no such thing. I means otherwise, how e.g. blind people
ought to be presented an idea of an "invisible picture"
in TEXTUAL representation.

And that must not make an error or noise on demand only.

You sure are not an eye dog in this group.



"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:11****************@PointedEars.de...
joe wrote:
Ok. I think I found a solution:

<img src="orig_image.jpg" onerror="this.src='alt_image.jpg';">
This is not a standards compliant solution, though. The `img' element
does not have an `onerror' attribute in Valid (X)HTML markup, and the
`alt' attribute is required. A standards compliant solution, taking
into account additional DOM features, could be

<!-- Transitional HTML or XHTML DOCTYPE, else use `id' instead of

`name' --> <html>
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function replaceOnError(img, alt_src)
{
if (typeof img.onerror != "undefined")
{
img.onerror = function()
{
this.src = alt_src;
return true;
}
}
}
</script>
...
</head>

<body onload="replaceOnError(document.images['foo'], 'alt_image.jpg');">
...
<img src="orig_image.jpg" alt="foo" name="foo">
...
</body>
</html>

But you rather should consider the ErrorDocument directive of httpd
compatible Web servers (like the Apache HTTP Server) which will also
work without client-side script support.
PointedEars


Jul 23 '05 #5
"commercial" schrieb:
[...]
"Alternative Image"

There is no such thing. I means otherwise, how e.g. blind people
ought to be presented an idea of an "invisible picture"
in TEXTUAL representation.
(This is the only part of your posting with some meaning, so I'll reply
to it.)

Well, the `alt' property of the respective HTMLImageElement and so the
`alt' attribute of the related `img' element can be changed as well.
That would result in different presentation on error for users without
image support, too.
And that must not make an error or noise on demand only.

You sure are not an eye dog in this group.
I am sure that you are not really know to whom you are replying, or
rather yelling, what.
[Top post, full quote]


You may also want to read the newsgroup's FAQ and FAQ notes.
PointedEars
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David | last post by:
everywhere I go, i get told "don't use frames, use php instead" ok... so how do i go about it? I want a top banner that doesn't move, and a "contents" type list on the left (that may have a...
3
by: McKirahan | last post by:
I said I wouldn't use "eval()" anymore but I need help to do it. Below is some stripped-down code (4 lines; watch for word-wrap) extracted from USGA.COM that preloads images: main_nav_home_F1...
2
by: Bettina | last post by:
How can I do to format the text shown in alt="..." Is it possible to define the size of the field?, colour of background, font, etc? Thank you Bettina
24
by: Markus Ernst | last post by:
Hi I have a title with a title gif: <h1><img src="mytitle.gif" alt="This is my title"></h1> So this is text-based browser and search-engine friendly and can be easily formatted with a style...
1
by: prasaddevivara | last post by:
I am using the outerHTML property to modify the HTML of existin elements in a web page in Internet Explorer. But same outerHTM property is not working in firefox browser, Anybody can tell me a...
7
by: Spartanicus | last post by:
Afaik the use of a <noscriptelement is frowned upon nowadays. For a JS random image changer I tried to use a replacement by having the script change the HTML src attribute value of an img element....
0
by: sachintandon | last post by:
Hello all, Thanks in advance for your help I have a problem in sending emails, my requirement is to send multipart alternative emails with attachments, I'm able to send text with attachments or...
5
by: crater | last post by:
Is there a way to get an HTML form to be submitted (via POST) by using an image button instead of a submit button for instance?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.