备选方案#1-使用额外
query string
确保图像不从缓存加载的一个非常简单的方法是添加额外的
查询字符串
作为参数(无需在
server side
),所以不可能是一样的
URL
被使用了两次(
在同一台计算机上
).
在这种情况下,我使用了
milliseconds since epoch
通过使用
Date().getTime()
.
var now = new Date().getTime();
$("#captchaText").attr('src', 'cryptographp.inc.php?intro=false&time=' + now);
这将导致请求
统一资源定位地址
例如这些:
cryptographp.inc.php?intro=false&time=1379998715616
cryptographp.inc.php?intro=false&time=1379998715618
cryptographp.inc.php?intro=false&time=1379998715636
etc...
备选方案#2-使用PHP禁用HTTP缓存
您也可以在
cryptographp.inc.php
使用以下代码(即使您选择
备选方案#1
):
header("Content-Type: application/json");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
喜欢
纳撒尼尔·格兰诺
评论中说,关于一些浏览器更改图像
src attribute
至相同
统一资源定位地址
可能不会产生新的
HTTP request
,因此建议您先将其切换到其他位置。
例如(JS):
$("#captchaText").attr('src', 'temp.png')
.attr('src', 'cryptographp.inc.php?intro=false');
-
附言-在这种情况下,我建议
temp.png
将是一个
真实文件
包含非常小的图像(例如
1px * 1px
透明图像)。