title: How to solve "ReferenceError", when loading script dynamically from external js file
body: <p>I have home.php and ext.js file. </p>
  
  <p>The procedure I want to make it, when I run home.php, I want to show dialog box.  When dialog box open, user can't access home.php before they click <code>button</code> from dialog box.</p>
  
  <p>I make dialog box in ext.js file and call that dialog procedure from home.php like this,</p>
  
  <pre><code>&lt;head&gt;
  &lt;script type="text/javascript" src="ext.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script&gt; 
  document.addEventListener("DOMContentLoaded", function(event) {
      create_dialog();
  }); 
  &lt;/script&gt;
  .....
  </code></pre>
  
  <p>In my ext.js file, I load jQuery and jQuery ui script dynamically.</p>
  
  <pre><code>//load jQuery
  var q = document.createElement("script");
  q.async = true;
  q.src = "jquery-1.11.3.min.js";
  var first = document.getElementsByTagName('script')[0];
  first.parentNode.insertBefore(q, first);
  
  //load jQuery ui
  var u= document.createElement("script");
  u.async = true;
  u.src = "jquery.ui/jquery-ui-1.11.4.js";
  var first = document.getElementsByTagName('script')[0];
  first.parentNode.insertBefore(u, first);
  
  function create_dialog() {
     $("&lt;div&gt;....&lt;/div&gt;").dialog({
        autoOpen:true,
        width:'600px',
        modal: true,
        resizable:false,
        draggable:false,
        buttons:........
      });
  }
  </code></pre>
  
  <p>All of above code is work before I clear the cache from browser history.</p>
  
  <p>But if I clear cache and run home.php again, I get error in firebug like this,</p>
  
  <pre><code>ReferenceError: $ is not defined
  $("&lt;div&gt;....&lt;/div&gt;").dialog({....
  </code></pre>
  
  <p>And sometimes I got above error and another error like this,</p>
  
  <pre><code>ReferenceError: $ is not defined
      $("&lt;div&gt;....&lt;/div&gt;").dialog({....
  
  ReferenceError: jQuery is not defined
       factory( jQuery );
  </code></pre>
  
  <p>When I got this problem, the dialog box cannot show and user can access <code>home.php</code>. Its problem for me. </p>
  
  <p>I already test with <code>loadScript(src,callback)</code> function but I got the same problem even I get callback success function. I don't know why?. </p>
  
  <p>I very appreciate for any suggestion. </p>