1
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
};

望ましい結果は、ブラウザがキャッシュされた image1 の代わりに image2 をキャッシュすることです。image2.src = someUrl;残念ながら、文の後でも image1 がキャッシュされます。

ただし、image1 をキャッシュし、セッション cookie を作成して手動で someUrl ページに移動することで機能します。次に、image2 をキャッシュします。

ページを更新せずにブラウザに画像を 2 回キャッシュさせることはできませんか?

4

2 に答える 2

5

URL に任意のパラメーターを追加してみてください。例えば ​​:

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

var image2 = new Image();
image2.src = someUrl + '?action=cache'; // or timestamp

これにより、ブラウザは image2 を新しい画像として扱い、正しい URL から画像を読み込みます (GET で他のパラメーターを送信していないと仮定すると、その場合は「?」の代わりに「&」を使用します)。

于 2013-06-06T15:46:55.757 に答える
0

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

于 2013-06-06T15:26:08.003 に答える