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

Optional Images

P: 2
Hi All,
I m wondering if there is a way in which to display an image only if the file exists. I dont want an alternate text to be displayed, nor the image container (or whatever that is called). Thus, if the image file doesnt exist, there should be no trace.

I cannot use server side programming. Can you please suggest if it can be solved using client-side scripting ?

Jul 15 '06 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function checkimages() {
  3.     var y = document.images;
  4.     var imglength = 0;
  5.     for (i=0;i<y.length;i++)
  6.     {
  7.         imglength = (y[i].fileSize)*1;
  8.         if (imglength < 0) {
  9.             y[i].width = 0;
  10.             y[i].height = 0;
  11.         }
  12.     }
  14. }
  15. </script>
  16. <body onload="checkimages();">
  17. <img src="raw.jpg">
  18. <img src="rock.jpg">
basicly it sets the image width and height to 0 if it has no file size... you wont see it but it will still be in the source... this is as close as i could come to it... hopes this help you out... the image raw.jpg was an image i didn't have and rock was an image i did have and everything seemed to work ok.
Jul 15 '06 #2

P: 2
singularly ingenious. It solves my case
Thanks a ton!!
Jul 16 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.