title: Problems with dynamic loading in JavaScript
body: <p>I am a JavaScript newbie and learn by working on a <em>pure</em> JavaScript "project" that calculates mathematical functions. It all works well. Now, as a further step, I want to make the messaging multilingual. The code should be capable of loading the appropriate language file at runtime. For the dynamic loading issue, I read and found solutions on Web pages like <a href="http://www.phpied.com/javascript-include/" rel="nofollow">this one</a>.</p>
  
  <p>Before writing the dynamic code, I loaded it statically and the test code worked well. The code I am asking for help about is just making the minor difference of loading a "script" element.</p>
  
  <p>The code where I run into problems is the <em>this.getString</em> function, where it is not possible to access the <em>de</em> element in the language file. At line <em>console.log(eval(language, tag));</em>, I get the error message "<em>Uncaught ReferenceError: de is not defined</em>".</p>
  
  <pre><code>//File: Utils/Lang/js/FileUtils.js
  function Language(language) {
      var __construct = function(dynamicLoad) {
          if (typeof language == 'undefined') {
              language = "en";
          }
          // Load the proper language file:
          loadFile("js/resources/lang.de.js");
          return;
      } ()
  
      this.getString = function(tag, strDefault) {
          console.log("getString(" + tag + ", " + strDefault + "): ");
          console.log("getString(...): document = " + document);
          console.log("getString(...): eval(" + language + ", " + tag + ") = ");
          console.log(eval(language, tag));
          var strReturn = eval('eval(language).' + tag);
  
          if (typeof strReturn != 'undefined') {
              return strReturn;
          } else {
              return (typeof strDefault != 'undefined') 
                  ? strDefault
                      : eval('en.' + tag);
          }
      }
  }
  </code></pre>
  
  <p>The static test code that works is not included, where I can access the de element.</p>
  
  <p>My question: How to load the language file properly so that the <em>de</em> tag is accessible?</p>
  
  <p>Thank you for your help!
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> //File: Utils/Files/js/FileUtils.js
   function loadFile(filepathname) {
       var reference = document.createElement('script');
  
       reference.setAttribute("type", "text/javascript");
       reference.setAttribute("src", filepathname);
  
       if (typeof reference != 'undefined') {
         document.getElementsByTagName("head")[0].appendChild(reference);
       }
       console.log("loadFile(\"" + filepathname + "\"): document = " + document);
     }
   //File: Utils/Lang/js/resources/lang.de.js:
   de = {
     pleaseWait: "Bitte warten..."
   };
  
   //File: Utils/Lang/js/resources/lang.en.js
   en = {
     pleaseWait: "Please wait..."
   };
  
   //File: Utils/Lang/js/TestLanguage.js:
   function output() {
     console.log("output()");
     var codes = ['de', 'en'];
  
     for (var i = 0; i &lt; codes.length; i++) {
       var translator = new Language(codes[i]);
  
       var message = "output(): in " + translator.getLanguage() + ": ";
  
       message += translator.getString('pleaseWait');
  
       console.log(message);
     }
   }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!--File: Utils/Lang/TestLang.html:--&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
    &lt;meta charset="ISO-8859-1"&gt;
    &lt;title&gt;Test languages&lt;/title&gt;
    &lt;script type="text/javascript" src="../Files/js/FileUtils.js"&gt;&lt;/script&gt;
  
    &lt;script type="text/javascript" src="js/Language.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="js/TestLanguage.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;button name="outputButton" onclick="output();"&gt;Click&lt;/button&gt;
    &lt;br&gt;Please press [F12] so that you can see the test results.
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>