title: Load and execute javascript code SYNCHRONOUSLY
body: <p>Is there a way to load and execute a javascript file in a synchronous way just like a synchronous XMLHttpRequest?</p>
  
  <p>I'm currently using a sync XMLHttpRequest and then eval for this, but debugging that code is very difficult...</p>
  
  <p>Thanks for your help!</p>
  
  <p><strong>Update</strong></p>
  
  <p>I tried this now:</p>
  
  <p>test.html</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript"&gt;
              var s = document.createElement("script");
              s.setAttribute("src","script.js");
              document.head.appendChild(s);
              console.log("done");
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>script.js</p>
  
  <pre><code>console.log("Hi");
  </code></pre>
  
  <p>Output:
  done
  Hi</p>
  
  <p>So it was not executed synchronously. Any idea to make "Hi" appear first?</p>
  
  <p><strong>Update 2</strong>
  Other example</p>
  
  <p>test.html (code inside a script tag)</p>
  
  <pre><code>var s = document.createElement("script");
  s.setAttribute("src","script.js");
  document.head.appendChild(s);
  SayHi();
  </code></pre>
  
  <p>script.js</p>
  
  <pre><code>function SayHi(){
      console.log("hi");
  }
  </code></pre>
  
  <p>Output: Uncaught ReferenceError: SayHi is not defined</p>