understanding jQuery $.getScript()

后端 未结 5 1577
情歌与酒
情歌与酒 2020-12-28 09:39

I use getScript to load dynamically my plugin:

$.getScript(\'js/code.photoswipe.jquery-3.0.4.min.js\', function () {
   //do magic
});
5条回答
  •  孤城傲影
    2020-12-28 10:29

    Your browser will cache the url accordingly already.. So you don't have to worry about caching.

    If however you want to bust the cache simply add a random string to the url like so:

    $.getScript('js/code.photoswipe.jquery-3.0.4.min.js?' + Math.random(), function () {
            //do magic
    });
    

    ?' + Math.random() will allow for a random number to append to your js file so caching is broken with each request for the file (as it randomly generates a number)

提交回复
热议问题