How to prevent the browser from caching a json file

后端 未结 4 1684
野趣味
野趣味 2020-12-04 17:09

So I\'m making this little project and I\'m having some troubles with catching. One thing that\'s not working is the browser keeps caching the json file that contains save d

4条回答
  •  执笔经年
    2020-12-04 17:32

    The easiest way is to append the source string with some random parameter, which gets ignored on the server side

    
    

    One solution would be to generate the script element using JavaScript and append the current time like this:

    var el = document.createElement( script );
    el.src = 'mySaveFiles.json?nocache=' + (new Date()).getTime();
    document.head.appendChild( el );
    

    That way, the browser will never cache the JSON-file as it appears to be a different file (due to the parameter) in every call.

提交回复
热议问题