【发布时间】:2010-01-04 06:52:39
【问题描述】:
<img src="test.php" />
其中 test.php 生成带有随机数的图像。
试过了:
$('#verifyimage').click(function() {
$(this).attr('src',$(this).attr('src'));
});
但它不起作用。
【问题讨论】:
标签: jquery
<img src="test.php" />
其中 test.php 生成带有随机数的图像。
试过了:
$('#verifyimage').click(function() {
$(this).attr('src',$(this).attr('src'));
});
但它不起作用。
【问题讨论】:
标签: jquery
您可以通过在末尾附加一个随机字符串来强制刷新,从而更改 URL:
$('#verifyimage').click(function() {
$(this).attr('src', $(this).attr('src')+'?'+Math.random());
});
【讨论】:
Cache-control、Expires 等。仅在收到 HTTP 请求后才起作用 - 以上只是强制客户端发出该请求。在没有Image.reload (AFAIK) 的情况下,这是发出新请求的最简单方法
添加时间戳或随机数:
var timestamp = new Date().getTime();
$(this).attr('src',$(this).attr('src') + '?' +timestamp );
【讨论】:
采用 KPrimes 的出色答案并添加跟踪器建议,这就是我想出的:
jQuery(function($) {
// we have both a image and a refresh image, used for captcha
$('#refresh,#captcha_img').click(function() {
src = $('#captcha_img').attr('src');
// check for existing ? and remove if found
queryPos = src.indexOf('?');
if(queryPos != -1) {
src = src.substring(0, queryPos);
}
$('#captcha_img').attr('src', src + '?' + Math.random());
return false;
});
});
【讨论】:
在 test.php 中将Content-Type: 的标头设置为image/jpeg
【讨论】: