Aminion
Aminion

Reputation: 465

How to reload image on page, with same URL?

I need to reload image on page. I trying this:

 $("#ill2d").attr("src", decision.Illustration + "#" + new Date().getTime());

But, image toggle on alt text. If I press F5, it work, and image is reloaded.

UPDATE I tried to do as advised:

$("#ill2d").attr("src", decision.Illustration + "?time=" + new Date().getTime());

But it still does not work, image toggle on alt text.

Upvotes: 0

Views: 1000

Answers (3)

Madd0g
Madd0g

Reputation: 4001

In your code example:

 $("#ill2d").attr("src", decision.Illustration + "#" + new Date().getTime());

The # is purely a client-side thing, it is not involved in url-based caching and is not sent to the server. Use a question mark instead and pass a query parameter.

Upvotes: 0

Al.G.
Al.G.

Reputation: 4387

A way to tell the browser you want to reload "another" image is to change the img src attribute. I suppose your img name is simple, like image.jpg, /path/to/img.png etc. (you do not have things like ?get=48).

You can make the browser "think" you want another image is displayed, you need to change the image url every time.

var img_src = 'path/to/image.png';

$('#ill2d').attr('src', img_src + '?rand=' + Math.random());

We store the original image src in a variable in order not to get url like this - path/to/img.png?123?456?789. (It is not wrong but seems weird)

Upvotes: 0

Eric
Eric

Reputation: 18922

Try this:

$("#ill2d").attr("src", $("#ill2d").attr("src") + "?" + (+new Date()));

Assuming the src is not changing, since you only want to refresh the current one set. This also asumes no timestamp currently are in the URI (no and-sign set).

Upvotes: 1

Related Questions