0

I currently have a Javascript file in called config.js in a dirtectory called static. The contents of the file is currently:

var config = {"config1": "test1", "config2": 1};

I also have the following function and it's associated call at the end of the script:

function updateConfig() {
    var fileRef = document.createElement('script');
    fileRef.setAttribute("type", "text/javascript");
    fileRef.setAttribute("src", "/static/config.js");
    headElem = document.getElementsByTagName("head")[0];
    headElem.appendChild(fileRef);
}

updateConfig();
test.innerText = config.config1;

However, when I run the script I get the following error in Firebug:

ReferenceError: config is not defined

How do I get my JS file to be loaded dynamically into my page? If I just put the link to config.js statically into the it works without a problem.

  • basic question, how did you link those js files in your project? – ddb Jul 28 '16 at 08:00
  • 1
    because test.innerText = config.config1; is evaluated before config.js is loaded – Olivier Boissé Jul 28 '16 at 08:00
  • The section titled "Detecting when the script has been executed" in the accepted answer of the duplicate question should be of particular interest. – Quentin Jul 28 '16 at 08:01
  • @Quentin thank you, I had been up and down so many of the questions that seemed to be duplicate and whatever my search terms were didn't bring up that solution. I'll give it a go and post it as an answer if I'm up and working. oliv37 seems like Quentins solution provides the answer to that, thank you. –  Jul 28 '16 at 08:10

0 Answers0