Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can IMG SRC tag be 'simplified'? 1

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
0
0
GB
Hi,

I have a webpage that displays a number of thumbnails. My problem is that the thumbnails are held on a different server to the website (to reduce bandwidth), so for example I've got:

<IMG SRC='
Is there anyway I can simplify this - when I've got 50 thumbnails I have the repeat the whole server path name when it's only the filename at the very end that changes each time.

For example, can I store the server name in a variable and stick the variable in the SRC tag?


- Andy
_______________________________
&quot;On a clear disk you can seek forever&quot;
 
nope, what you could do tho is place the page with all the thumbnails on that other server and when you link into the picture page just redirect your users to the other server.
 
Try this:

<html>
<head>
<script language=&quot;javascript&quot;>
var gsPath=&quot;
//replace with your thumb names, add additional ones as you need
var garrImageNames = new Array(&quot;thumb1&quot;,&quot;thumb2&quot;,&quot;thumb3&quot;,&quot;thumb4&quot;,&quot;thumb5&quot;);

function BuildImgSrc(){
var i;

//update each image src
for(i=1;i<=5;i++){
//get img tag
var objImg=document.getElementById(&quot;img&quot;+i);

objImg.src=gsPath+garrImageNames[i-1]+&quot;.jpg&quot;;
}
}

</script>
</head>
<body onload=&quot;BuildImgSrc()&quot;>
<img id=&quot;img1&quot; src=&quot;&quot;>
<img id=&quot;img2&quot; src=&quot;&quot;>
<img id=&quot;img3&quot; src=&quot;&quot;>
<img id=&quot;img4&quot; src=&quot;&quot;>
<img id=&quot;img5&quot; src=&quot;&quot;>
</body>
</html>

In your code remember to add id attributes to your image tags.

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
MrGreed's approach will just break your images for anybody surfing with Javascript switched off.

You could try using the <base> tag (see ) to make all your URLs relative to the other server, though you'll then have to use full URLs for other links on the page. Personally, I wouldn't bother - just cut & paste the URL as required. I'd also want to be sure that it was worth the hassle to move all those images to another server - consider keeping the thumbnails on your main server and just hosting the full-size images elsewhere.

-- Chris Hunt
 
Thanks MrGreed!

This was exactly what I was looking for. Since my pages already expect users to have Javascript enabled it's the ideal solution.

I appreciate that hosting the thumbnails on the same server as the pages would be the best solution, however I get around 60000 page hits a day and each page has an average of 20 thumbnails, so that generates a lot of bandwidth usage and I get a monthly limit of 20Gb which would soon be used up.


- Andy
_______________________________
&quot;On a clear disk you can seek forever&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top