title: Output the contents of a JavaScript file as an alert or log in console
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/148441/how-can-i-get-the-content-of-the-file-specified-as-the-src-of-a-script-tag">How can I get the content of the file specified as the &#39;src&#39; of a &lt;script&gt; tag?</a>  </p>
  </blockquote>
  
  
  
  <p>this may seem like a strange question but it's been on my mind. Say I had a HTML file that references one or many JavaScript files, these files are local but could be external. Now for some reason (my own curiosity) I wish to output the contents of one of these files like a string and write it to the console or even <code>alert</code> the contents. So say I have the following JS file called jsFile.js:</p>
  
  <pre><code>// JavaScript Document
  var testString = "I am just an output... or am I",
      testNumber = 1,
      testArray = [],
      testObject = {};
  
  // random functionality, etc... etc...
  if(testNumber &gt; 100){
      // do something...
  }
  </code></pre>
  
  <p>and I want to output this like so when opening my HTML page:</p>
  
  <p><img src="https://i.stack.imgur.com/52pzX.png" alt="alert image"></p>
  
  <p>however I am unsure how to do this, can I find the <code>SCRIPT</code> tag in the dom and use a method on it to output it's contents (see below) or do I have to read the file (somehow) then loop through each line of code collecting it in a variable, then output it by either <code>alert</code> or <code>console.log</code></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Hello World&lt;/title&gt;
  &lt;/head&gt;
  &lt;script type="text/javascript" src="jsFile.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  // find the JS node...
  window.onload = function(){
  
      var theFile = document.getElementsByTagName("script")[0];
  
      // none of these will work as the code within the jsFile.js is not a DOM object...
      console.log(theFile.text); // returns a zero length string
      console.log(theFile.innerHTML); // returns a zero length string
      console.log(theFile.textContent); // returns a zero length string   
  
  }
  
  &lt;/script&gt;
  &lt;body&gt;
  I am just a HTML file... no more, no less...
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Above is my first attempt however none of these methods will work as the contents of the script are not DOM objects. I don't need a code specific answer, just a proof of concept, idea or point in the right direction. If I'm not making sense please say so and I will reword my question.</p>