2

I want to dynamically reload a local JSON File. This JSON data is updated by another program. I found following solution for the problem here: https://stackoverflow.com/a/17736371/3945764 my code is similar to this. The other program witch is calling the loadJsFile Function after updating the JSON. My problem now is that it dosn't load the updated JSON it just adds one more "script" element in HTML but the JSON data is still the old. In my JSON file is a var jsonObject. Anyone have any ideas?

function loadJsFile(){
    var fileref = document.createElement('script');
    var filename = 'myjson.json';
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    fileref.onload = dataIsLoaded;

    if (typeof fileref!="undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }  
}

function dataIsLoaded(){
    console.log(jsonObject);
}
Community
  • 1
  • 1
Matthias
  • 351
  • 5
  • 19
  • 2
    It's probably because the `.json` file is being cached. [Set proper no-cache headers](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers), or append a timestamp to your `filename`, e.g. `var filename = 'myjson.json?' + (new Date()).getTime();` – Matt Sep 15 '14 at 09:27

0 Answers0