Tutorial :Getting content of a script file using Javascript


I have the following script element in my web page:

<script src="default.js" type="text/javascript"></script>  

Using JavaScript, I want to be able to retrieve the content of the script file. I know I could use a ajax request to get the data but then I am getting something from the server that I already have locally.

So what I would prefer to do is retrieve the content from the DOM (if that's possible) or something that has the same result.

Cheers Anthony


I was trying to simplify the question, maybe a bad a idea, I thought this way would cause less questions.

The real situation I have is as follows, I actually have

<script type="text/html" class="jq-ItemTemplate_Approval">     ...     html template that is going to be consumed by jQuery and jTemplate     ...  </script>  

Now this works fine but it means each time the page loads I have to send down the template as part of the html of the main page. So my plan was to do the following:

<script src="template.html" type="text/html"></script>  

This would mean that the browser would cache the content of template.html and I would not have to send it down each time. But to do this I need to be able to get the content from the file.

Also in this case as far as I know requesting the content via ajax isn't going to help all that much because it has to go back to the server to get the content anyway.


I think what you want to do is to assign a variable inside template.js. Then you have the variable available for use wherever you want in jquery. Something like:

var tpl = "<div> ... </div>"  

Wouldn't this be a simpler solution to your problem? We do this in Ext JS. I think this will work for you in jQuery.


If I understand you correctly, you don't want to use Ajax to load an html template text, but rather have it loaded with the rest of the page. If you control the server side, you can always include the template text in an invisible div tag that you then reference from Javascript:

<div id="template" style="display:none;">  ...template text...  </div>  <script>  // pops up the template text.  alert(document.getElementById("template").innerHTML);  </script>  

If you are just looking for to load the template so that you can have it cached, you can put the contents in a variable like this:

<script>  var template = "template text..";  </script>  

or you can load it using ajax and store the template in a variable so it is accessible. It's pretty trivial in jquery:

var template;  $.get("template.html", function(data){    template = data;  });  


unless you load a script as literal text in the page, it does not exist as text. It is interpreted by the browser and melded into the runtime, with any other scripts.

If you want the source you have to fetch it again,if with Ajax get the responseText.

It will come from the browser cache, and doesn't have to be downloaded again.


You could get the attribute of the src of the script and then use XHR to get the contents of the JS file. It's a much cleaner way of doing it IMO. e.g.:-

if(window.XMLHttpRequest) {                  var xhr = new XMLHttpRequest();                           xhr.onreadystatechange = function() {                      if(xhr.status == 200 && xhr.readyState == 4) {                          var sourceCode = xhr.responseText;                                              alert('The source code is:-\n'+sourceCode);                      }                  }                  xhr.open("GET",document.getElementById('scriptID').src,true);                  xhr.send(null);              }  


Using an iFrame & HTML5 Local Storage

Save the templates for rendering later...

not stoked about the iFrame, but it seems to be working pretty good (haven't ran performance tests yet)

Put the iFrame on the page you want the template on (index.html)

<html>    <head>      <iframe src="mustache.Users.html" onload="this.remove();" class="hidden" id="users_template"></iframe>    </head>  </html>  
  • Make sure the src attribute is set
  • hide the element until you can get rid of it after it loads

Put this body wrapper around your template (mustache.Users.html)

(don't worry it won't show up in the template)

<body onload="localStorage.setItem('users_template',this.document.body.innerHTML);">     <ul class="list-group" id="users" >      {{#users}}<li>{{name}}</li>{{/users}}    </ul>  </body>  
  • replace 'users_template' with whatever name for your variable
  • the 'onload' attribute saves the template into localStorage during load

Now You can access your templates from anywhere




check out this image to see it in Chrome


What is in the JavaScript file? If it's actual code, you can run functions and reference variables in there just like you had cut and paste them into the webpage. You'll want to put the include line above any script blocks that reference it.

Is this what your looking to accomplish?


Why not use Ajax (well Ajah because its html :-))?

when the server is set up correctly and no no-cache or past expires headers are sent, the browser will cache it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »