我正在访问我的网站上的一个链接,每次访问它都会提供一个新的图像。
我遇到的问题是,如果我试图在后台加载图像,然后更新页面上的图像,图像不会改变——尽管在我重新加载页面时它会更新。
var newImage = new Image();
newImage.src = "http://localhost/image.jpg";
function updateImage()
{
if(newImage.complete) {
document.getElementById("theText").src = newImage.src;
newImage = new Image();
number++;
newImage.src = "http://localhost/image/id/image.jpg?time=" + new Date();
}
setTimeout(updateImage, 1000);
}
FireFox看到的头文件:
HTTP/1.x 200 OK
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: image/jpeg
Expires: Fri, 30 Oct 1998 14:19:41 GMT
Server: Microsoft-HTTPAPI/1.0
Date: Thu, 02 Jul 2009 23:06:04 GMT
我需要强制刷新页面上的图像。什么好主意吗?
我有一个要求:1)不能添加任何?var=xx的图像2)它应该跨域工作
我真的很喜欢这个答案中的第4个选项,但是:
它在可靠地跨域工作方面存在问题(并且需要修改服务器代码)。
我的捷径是:
创建隐藏iframe
加载当前页面到它(是的,整个页面)
iframe.contentWindow.location.reload(真正的);
重新设置图像源为其本身
在这里
function RefreshCachedImage() {
if (window.self !== window.top) return; //prevent recursion
var $img = $("#MYIMAGE");
var src = $img.attr("src");
var iframe = document.createElement("iframe");
iframe.style.display = "none";
window.parent.document.body.appendChild(iframe);
iframe.src = window.location.href;
setTimeout(function () {
iframe.contentWindow.location.reload(true);
setTimeout(function () {
$img.removeAttr("src").attr("src", src);
}, 2000);
}, 2000);
}
我知道,setTimeout。您必须将其更改为适当的onload-events。
基于Doin的#4代码,下面的示例利用文档简化了代码。在iframe中写入而不是src来支持CORS。也只关注破坏浏览器缓存,而不是重新加载页面上的每个图像。
下面是用typescript编写的,并使用了angular的$q promise库,只是供参考,但应该很容易移植到普通javascript。方法意味着存在于typescript类中。
返回一个承诺,该承诺将在iframe完成重新加载时得到解决。没有经过严格测试,但对我们来说很好。
mmForceImgReload(src: string): ng.IPromise<void> {
var deferred = $q.defer<void>();
var iframe = window.document.createElement("iframe");
var firstLoad = true;
var loadCallback = (e) => {
if (firstLoad) {
firstLoad = false;
iframe.contentWindow.location.reload(true);
} else {
if (iframe.parentNode) iframe.parentNode.removeChild(iframe);
deferred.resolve();
}
}
iframe.style.display = "none";
window.parent.document.body.appendChild(iframe);
iframe.addEventListener("load", loadCallback, false);
iframe.addEventListener("error", loadCallback, false);
var doc = iframe.contentWindow.document;
doc.open();
doc.write('<html><head><title></title></head><body><img src="' + src + '"></body></html>');
doc.close();
return deferred.promise;
}
我通过servlet将数据发送回来解决了这个问题。
response.setContentType("image/png");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache, must-revalidate");
response.setDateHeader("Expires", 0);
BufferedImage img = ImageIO.read(new File(imageFileName));
ImageIO.write(img, "png", response.getOutputStream());
然后从页面中给它一个带有一些参数的servlet,以获取正确的图像文件。
<img src="YourServlet?imageFileName=imageNum1">
我改进了AlexMA的脚本,在网页上显示我的摄像头,并定期上传同名的新图像。我有问题,有时图像闪烁,因为一个破碎的图像或不完整(向上)加载的图像。为了防止闪烁,我检查了图像的自然高度,因为我的网络摄像头图像的大小没有改变。只有当加载的图像高度符合原始图像高度时,完整的图像才会显示在页面上。
<h3>Webcam</h3>
<p align="center">
<img id="webcam" title="Webcam" onload="updateImage();" src="https://www.your-domain.com/webcam/current.jpg" alt="webcam image" width="900" border="0" />
<script type="text/javascript" language="JavaScript">
// off-screen image to preload next image
var newImage = new Image();
newImage.src = "https://www.your-domain.com/webcam/current.jpg";
// remember the image height to prevent showing broken images
var height = newImage.naturalHeight;
function updateImage()
{
// for sure if the first image was a broken image
if(newImage.naturalHeight > height)
{
height = newImage.naturalHeight;
}
// off-screen image loaded and the image was not broken
if(newImage.complete && newImage.naturalHeight == height)
{
// show the preloaded image on page
document.getElementById("webcam").src = newImage.src;
}
// preload next image with cachebreaker
newImage.src = "https://www.your-domain.com/webcam/current.jpg?time=" + new Date().getTime();
// refresh image (set the refresh interval to half of webcam refresh,
// in my case the webcam refreshes every 5 seconds)
setTimeout(updateImage, 2500);
}
</script>
</p>
作为……的替代方案。
newImage.src = "http://localhost/image.jpg?" + new Date().getTime();
...看来……
newImage.src = "http://localhost/image.jpg#" + new Date().getTime();
...足以欺骗浏览器缓存而不绕过任何上游缓存,假设您返回了正确的cache - control头。虽然你可以用…
Cache-Control: no-cache, must-revalidate
...你就失去了If-Modified-Since或If-None-Match标头的好处,所以…
Cache-Control: max-age=0, must-revalidate
...应该防止浏览器重新下载整个图像,如果它实际上没有改变。在IE, Firefox和Chrome上进行测试和工作。令人恼火的是,它在Safari上失败了,除非你使用…
Cache-Control: no-store
...尽管这可能比用数百个相同的映像填充上游缓存更可取,特别是当它们运行在您自己的服务器上时。: -)
更新(2014-09-28):现在看起来像是Cache-Control: Chrome也需要no-store。