Solved

How to prevent caching a .GIF image in browser?

Posted on 2002-07-25
10
890 Views
Last Modified: 2008-03-06
I am dynamically generating a GIF image, but have run into a problem. Because it is always generated under the same filename, my browser's cache gets in the way -- it works great the first time, but each additional time the cached GIF is displayed, not the newly generated GIF. If I clear the browser cache, it works fine again, but obviously only once.

Is there something I can specify on the image tag to prevent the browser (Netscape, in this case) from caching a GIF? The only option I can think of is to dynamically write to a different filename each time, but that complicates things, so I would like to avoid it.

BTW, before you start pointing out problems with this design (like it is CPU intensive, or simultaneous users would clobber each other's images on the server), let me say that this is strictly an admin function, run by a single individual -- I really do have valid reasons to do it this way.


Thanks in advance,
parkerea
0
Comment
Question by:parkerea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7178390
add a random parameter to the src of your img tag.  For example, using ASP do this:

<IMG src="somepage.asp?rnd=<%=rnd()%>">
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7178397
by "random parameter" I mean something that will change every time ...


if you are generating the URL on the client, to this:

var myImg = new Image();
myImg.src = "somepage.asp?rnd=" + Math.random();
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7178524
try following:
<META HTTP-EQUIV="Expires"       CONTENT="Fri, Jun 12 1981 08:20:00 GMT">
<META HTTP-EQUIV="Pragma"        CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:parkerea
ID: 7182923
Thank you for your replies.

ahoffmann: the meta tags did not to help. I also did some searching on the web, and found mention of using header info for cache control, but that did not seem to help either. The page itself is apparently not being cached, but the images on it are.

knightEknight: unfortunately, we are running Linux, so ASP is not an option. Also, if I understand your suggestion, you are talking about changing the filename each time, which we would like to avoid. We really want to just prevent caching the image, if possible.


Thanks again,
parkerea
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7183733
then you need to disable the cache of the browser
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 100 total points
ID: 7185136
I don't mean change the name of the file ... I mean add a parameter to the image URL that is unique every time ...

Can I assume that you are using a CGI to generate the HTML?  If so, then have the CGI generate a unique parameter for the image src (like a random number or the time in milliseconds).  The parameter doesn't do anything ... but it does make the URL unique (and therefore not cached).  

If you are not using a CGI then you can do it with client-side script like this ... first, in your code, define the image, but don't give it a src:

<IMG name='myimage' border='0' width='300' height='200'>

then when the page loads, load the image, adding a unique parameter:

<BODY onLoad='document.images["myimage"].src="http://server.com/images/myimage.gif?rnd="+Math.random();'>

0
 
LVL 27

Expert Comment

by:BigRat
ID: 7187644
I suppose you use rewrite to get a .gif ending into CGI?

I also suppose that your cgi generates a

     Content-Type: image/gif

line?

If so, generate also :-

    Cache-Control: no-cache
    Expires: Fri, Jun 12 1981 08:20:00 GMT

or anyother date in the past (your birthday for exxample). These two lines (terminated with CR/LF) can be put before or after the Content-Type but BEFORE the famous "blank line".

HTH
0
 

Author Comment

by:parkerea
ID: 7188124
knightEknight:

Now that I understand it, I think your solution will work, but my development PC just went into the shop for a bit... I will test and let you know the results when I get it back -- probably a week or better.

Yes, both the HTML and the GIF image are being generated by a CGI.


Thanks again,
parkerea
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7271008
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I may recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
0
 

Author Comment

by:parkerea
ID: 7283112
Sorry I took so long -- after I got my machine back I had lost track of things. Thank you for the help.


- parkerea
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Selected in an option list 13 27
Sending ASP to server side 8 32
Change how page is organized 3 37
HTML auto refresh to another site 4 30
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question