[Javascript] Refresh an image, change an image

Chris T christ at saeweb.com
Fri Jun 11 08:09:32 CDT 2004


> 1st question:
>
>
> <meta HTTP-EQUIV="Refresh" content="240">
>
> I am familiar with the above instruction that will refresh my page's
entire
> contents every 240 seconds.
>
> but what I am wondering is this: Is it possible to refresh JUST one image
on
> the page, such as the one below...
>
> <div id="live_image">
> <img src="http://www.company.com/images/live_image.gif" width="741"
> height="631" alt="" name="itsalive">
> </div>
>
>
> My reason for asking is that the page has quite a few other images, but
none
> of them need refreshing. Loading them all again would slow things down,
and
> is really un-necessary.


By refresh, do you mean the image itself will have changed and you need to
reload it, or do you want to update the src of the image to point to a new
one?

Basically, will it always point to "live_image.gif" or is that going to
change?

If it stays the same, I don't know how it would handle requesting an image
that it thinks it already has. I guess you could just use the regular
technique and see what happens:

<script>
    var objImg = document.images["itsalive"]
    objImg.src = "/images/live_image.gif"
    // Change the src to whatever image you want to display
</script>

>
> ******************************************
> 2nd question
>
> Assuming I want to change the above image on the page for a new one on my
> server, (not previously downloaded), such as
> http://www.company.com/images/live_image2.gif what would be the code for a
> form-button that would do that ?
>
>
> Note that pre-loading both images isn't an option as they are generated on
> the server.
>
> Tim in Ireland.

Form-button?  Not sure what you mean, but if you're saying you want a
user-generated event to come from a button, then I say you take the above
code, functionalize it, and use a button (or text link) to run the function

<script>
    function ChangeImage(){
        var objImg = document.images["itsalive"]
        objImg.src = "/images/live_image.gif"
    }
</script>

<input type="button" onClick="ChangeImage()" value="Click here" />

If that's not what you're looking for, perhaps you can tell me where I
misunderstood you.

Chris Tifer
http://emailajoke.com




More information about the Javascript mailing list