title: How to append <script> to Head or Body Using Javascript?
body: <p>I have a JavaScript problem.</p>
  
  <p>I searched on google how to append  using javascript and I found an example. I want to manually append a <code>script</code> tag to head or body, but the example just appends in the head.</p>
  
  <p>I edited the example so that the script tag can be appended in the head or body, but the code doesn't work.</p>
  
  <p>Can anyone help me fix my problem? And please show me the demo on jsfiddle :D</p>
  
  <p>Thanks :)</p>
  
  <p>Here my script:</p>
  
  <pre><code>//JS
  //Defined with  loadjscssfile("yourcssurl.js", "js", "head"); or addCss("yourcssurl.js", "js", "body");
  //CSS
  //Defined with  loadjscssfile("yourcssurl.css", "css", "head"); or addCss("yourcssurl.css", "css", "body");
  function loadjscssfile(filename, filetype, pos) {
      if (filetype == "js") { //if filename is a external JavaScript file
          var fileref = document.createElement('script')
          fileref.setAttribute("type", "text/javascript")
          fileref.setAttribute("src", filename)
      }
      else if (filetype == "css") { //if filename is an external CSS file
          var fileref = document.createElement("link")
          fileref.setAttribute("rel", "stylesheet")
          fileref.setAttribute("type", "text/css")
          fileref.setAttribute("href", filename)
      }
  
      if (typeof fileref == filetype) {
          document.getElementsByTagName(pos)[0].appendChild(fileref)
      }
  
      else if (typeof fileref != "undefined") {
          document.getElementsByTagName("head")[0].appendChild(fileref)
      }
  }
  
  loadjscssfile("myscript.js", "js", "body") //dynamically load and add this .js file
  loadjscssfile("javascript.php", "js", "body") //dynamically load "javascript.php" as a JavaScript file
  loadjscssfile("mystyle.css", "css", "body") ////dynamically load and add this .css file
  </code></pre>
  
  <p>And here is the original script, before I edited: <a href="http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml" rel="nofollow">Dynamically loading an external JavaScript or CSS file</a></p>
  
  <p>Sorry for my bad english :)</p>