• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[Javascript] XHTML Compliant img onError?

SPeedY_B

I may actually be insane.
#1
Presently, I have the following code to replace images which are not available:
Code:
[b]Head[/b]
<script language="JavaScript" type="text/javascript">
function noImage(theImage) { theImage.src = "images/offline.png"; }
</script>

[b]Img Code[/b]
<img src="[i]whatever[/i]" alt="img" onError="noImage(this);" />
which works perfectly, however onError within an img tag isn't XHTMLv1.1 compliant, therefore does anyone know of a different way this can be done, which is compliant?
 

Khayman

I'm sorry Hal...
Political User
#5
SPeedY_B said:
Presently, I have the following code to replace images which are not available:
Code:
[b]Head[/b]
<script language="JavaScript" type="text/javascript">
function noImage(theImage) { theImage.src = "images/offline.png"; }
</script>

[b]Img Code[/b]
<img src="[i]whatever[/i]" alt="img" onError="noImage(this);" />
which works perfectly, however onError within an img tag isn't XHTMLv1.1 compliant, therefore does anyone know of a different way this can be done, which is compliant?
XHTML 2.0 allows you to specifiy another image (or anything else) as the alt... not much help maybe, but somthing to think about

*looks around*
 

SPeedY_B

I may actually be insane.
#8
Line 68, column 194: there is no attribute "onerror"

...1px solid white;margin:0px;" onerror="javascript:noImage(this);" />
You're right.

Just had a quick read on XHTML 2.0, seems interesting... no img tags, etc. :)
 
#9
yeah XHTML 2.0 will use the object tag and you will specify a mime type for the object so for a jpeg image you would use something like
<object type="image/jpeg" href="image.jpg"/>

problem though for internet explorer though as it assumes the object tag is for embedding activex controls.... yay
 

SPeedY_B

I may actually be insane.
#10
It sure does. Stupid thing. IE6 is causing me an evil headache tonight, f---ing thing.

This javascript is needed for the new version of Camportal (w00!) which works great with Gecko and KHTML, yet when it comes to IE, it all goes wrong, and I really give up trying to fix it.

Anyway, back on topic....

:p
 

SPeedY_B

I may actually be insane.
#14
FishBoy said:
try this,
Code:
<img name="img1" src="blah.jpg" alt="Picture" onError="document.images.img1.src='blah2.jpg';/>
That's not going to work, onerror is the attribute causing the problem.
 

X-Istence

*
Political User
#17
XHTML 2.0 will allow for this:

Code:
<object data="rides.mpeg" type="application/mpeg">
    <object data="rollercoaster.jpg" type="image/jpg">
        Jack tries to expand his horizons on the racing coasters.
    </object>
</object>
Which means that if the mpg can't be loaded it tries the image, if the image can not be loaded, it shows the text. Same can be used to have an image as a fall back if another does not exist.

I am starting to like XHTML 2.0 allready :p
 

j79zlr

Glaanies script monkey
Political User
#18
You are using php, why not just have it check for the existence of the image, and if it doesn't exist, have it load the noimage image.

PHP:
<?php
$image_path='/path/to/image.jpg';
if (file_exists($image_path)) {
  echo "<img src=\"$image_path\" alt=\"img\" />";
}
else {
  echo "<img src=\"images/offline.png\" alt=\"img\" />";
}
?>
 

SPeedY_B

I may actually be insane.
#20
Of course they're remote images.

Yes, I've already considered making the server remotely connect to each to check it's existence, but with a possible 50+ images viewable, and multiple users, it's a bit pointless.
 

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,247
Members
89,018
Latest member
debbieherrin