Hi All, I am trying to use an Image on a page fr about 40 times. Is there a way to avoid writing src on all images. So that change can be done at a single point. I have tried Css with background property set. But I would like alt to work as well.

4 answers

Doug 1095

Fourtyfreak mentions using Server Side Includes. There are many ways to accomplish this server-side, and the most common is probably PHP:

$img_url = '/imagefolder/image.png';
$img_alt = 'Alternative text description';

<img src="<?php echo $img_url; ?>" 
   alt="<?php echo $img_alt; ?>">

But before you jump into that, you should ask yourself why you have a single image repeated dozens of times on a page with the alt attribute required. Is the image actual content that must be repeatedly described to non-visual users? Or is it merely decoration? If the latter, stick with CSS background property.

Answered almost 7 years ago by Doug
danwellman 5600

You could do it with JavaScript pretty easily, but what if JS is disabled on the client...Copy and paste is your friend ;)

Answered almost 7 years ago by danwellman

You could try using a Server Side Include, not the most elegant solution, but may help you.

Bung this where you want the images to appear:

<!--#include virtual="/includes/image.ssi" -->

Then create a small file (image.ssi) to contain the code for the image.

<img src="image.png" alt="Image Alt Text" />

Your original page will need to renamed to have an .shtml extension though, and will only work if your webhost supports SSI's.

Answered almost 7 years ago by Fourtyfreak

PHP or some other server side scripting would be the best answer. If you decide that is not the route to go, then use a text editor with the ability to find and replace with regular expressions. I recommend vim. Alternately, you could just use sed to make the replacement in all of the files for you. If it were me, I would use sed to replace all instances of the the src with the PHP code and use that from now on.

sed -ie 's/src="foo.png"/src="<?php echo $my_img; ?>"/' 's/alt="Old text"/alt="<?php echo $my_text; ?>"/'  *.htm?
Answered almost 7 years ago by kainosnous