How to refresh the src of <img /> with jQuery?

Viewed 58498
<img src="test.php" />

where test.php generates an image with a random number.

Itried :

$('#verifyimage').click(function() {
        $(this).attr('src',$(this).attr('src'));
    });

But it doesn't work.

4 Answers

You can force a refresh by appending a random string at the end, thus changing the URL:

$('#verifyimage').click(function() {
    $(this).attr('src', $(this).attr('src')+'?'+Math.random());
});

Add a timestamp or a random number:

var timestamp = new Date().getTime();
$(this).attr('src',$(this).attr('src') + '?' +timestamp );

In test.php set the headers of Content-Type: to image/jpeg

Related