使用javascript创建两次缓存图像

use*_*613 1 javascript php caching

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
};
Run Code Online (Sandbox Code Playgroud)

期望的结果是让浏览器将image2缓存为缓存的image1的替代.不幸的是,即使在image2.src = someUrl;语句之后,image1也是缓存的.

然而,有效的是缓存image1,然后创建会话cookie并手动转到someUrl页面.然后它会缓存image2.

是否无法在不刷新页面的情况下使浏览器缓存两次图像?

Ska*_*ski 5

您可以尝试将任意参数添加到网址.例如 :

var image1 = new Image();
image1.src = someUrl;

var image2 = new Image();
image2.src = someUrl + '?action=cache'; // or timestamp
Run Code Online (Sandbox Code Playgroud)

这将欺骗浏览器处理图像2作为一个新的形象,同时还从加载正确的网址,图像(假设你是不是在GET发送任何其他PARAMATERS,在这种情况下使用"&",而不是"?")