[Javascript] XHTML Compliant img onError?

SPeedY_B

I may actually be insane.
Joined
31 Mar 2002
Messages
15,807
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?
 
there could be a css way to do it, but I dont know of any off the top of my head though.
 
Since attributes have to be lowercase, try onerror rather than onError.
 
Zedric said:
Since attributes have to be lowercase, try onerror rather than onError.
It's not the case, just the fact that "onerror" doesn't exist as an attribute:
Line 68, column 194: there is no attribute "onerror"
:)
 
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*
 
*declares document XHTML 2.0*

Hmmm, would be rather handy.
 
try
Code:
onerror="javascript:noImage(this);"

don't think that will work
 
Last edited:
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. :)
 
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
 
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
 
Offtopic:

Mind pointing me in the direction of that XHTML topic?
 
try this,
Code:
<img name="img1" src="blah.jpg" alt="Picture" onError="document.images.img1.src='blah2.jpg';/>
 
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.
 
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
 
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\" />";
}
?>
 
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 profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back