用javascript创建两次缓存图像
Create cached image twice with javascript
var image1 = new Image();
image1.src = someUrl;
// someUrl is a valid URL to a PHP file which outputs a PNG file using the imagepng function. This action caches image1 to someUrl.
image1.onload = function() {
// Some things have changed and a new image is created, which I would like to cache to someUrl.
// This is currently done by sending a session (temporary) cookie, which is recognised by the PHP file, so that it knows to take different action than the first time.
// Again, the imagepng function is used to output the image.
var image2 = new Image();
image2.src = someUrl; // this is the exact same URL
};
期望的结果是让浏览器缓存image2作为缓存的image1的替换。不幸的是,image1是缓存的,甚至在image2.src = someUrl;
语句之后。
的工作原理是缓存image1,然后创建会话cookie并手动转到someUrl页面。然后缓存image2。
是不是不可能让浏览器缓存图像两次,而不刷新页面?
您可以尝试在url中添加任意参数。例如:
var image1 = new Image();
image1.src = someUrl;
var image2 = new Image();
image2.src = someUrl + '?action=cache'; // or timestamp
这将欺骗浏览器将image2视为新图像,同时仍然加载该图像(假设您没有在GET中发送任何其他参数,在这种情况下使用'&'而不是'?')
尝试在输出png的php文件中使用缓存控制头。例如
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
来源:http://php.net/manual/en/function.header.php
相关文章:
- Meteor Router数据函数被调用两次
- 从MySQL数据库中获取输入数据需要两次页面刷新
- Module.start()已激发两次
- Jquery点击事件必须点击两次
- Append元素在运行两次函数后不显示
- 防止双击执行两次jQuery post请求
- 我必须点击两次才能激活任何按钮操作(离子/角度)
- 单击jQuery会激发两次
- 如何避免在树上走两次
- button.单击两次删除附加操作后不工作
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 单击元素两次后执行操作
- AngularJs正在阻止链接被点击两次
- 为什么我的路线处理程序会触发两次
- 为什么Highchart在我的rails应用程序中加载了两次?(未捕获的Highcharts错误#16)
- 在列表中至少使用两次随机生成的nr
- 检查某个键是否按下两次
- AngularJS.使用缓存时,防止加载JSON数据两次
- 用javascript创建两次缓存图像
- 在Javascript中缓存只使用两次的对象是否明智?