LoginSignup
0
0

More than 5 years have passed since last update.

jQueryでページ内全画像のsrc属性の末尾に適当な文字列(ランダムの数字)をつける

Last updated at Posted at 2018-12-14

画像をキャッシュさせず、ページ表示のたびロードさせたい時、なおかつ、楽天GOLDのようなサーバサイドではなく、ブラウザ側だけで対処したい時に。

$(function(){

    //ランダムの数字を作る
    var rand = parseInt(Math.random()*10000);

    $('img').each(function(){
        //imgのsrc属性取得
        var src = $(this).attr('src');
        //src末尾に?xxxx のランダム数字を追加
        var src_added = src + '?' + rand;
        //src属性の書き換え
        $(this).attr('src',src_added);
    });

});
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0