samedi 13 juillet 2019

How do I pass this variable to global? Treating it like a function is not working [duplicate]

This question already has an answer here:

I am trying to pass the variable json out to global. I've looked up how to do this with functions, but that does not seem to work in this situation. How would I go about getting json out of this "function?" to be global.

I've truncated the function xml2json for easier readability.

document.addEventListener('DOMContentLoaded', ()=>{
      //fetch the data as soon as the page has loaded
      let url = "schedule.xml";
      fetch(url)
      .then(response=>response.text())
      .then(data=>{
          //console.log(data);  //string
          let parser = new DOMParser();
          let xml = parser.parseFromString(data, "application/xml");
          console.log(xml);

          function xml2json(xml, tab) {}

          json = xml2json(xml);
          json = json.replace('undefined','');


          json = JSON.parse(json);
          console.log(json);
          return json;
      });
      return data;
  });

var json = data;
document.write(json);




Aucun commentaire:

Enregistrer un commentaire