views:

699

answers:

7

Hi guys

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

UPDATE

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.

A: 

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?

ICodeForCoffee
A: 

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.

kennebec
If this is the case, could I put this "<script src="template.html" type="text/html"></script>" in the top of my page and then call template.html via ajax and have it use the cached version that was gotten previously?
vdh_ant
A: 

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;
});
Helgi
In order of the points you raised:1) I could do this but it wouldn't take advantage of the browser ability to cache externally referenced files. Some of my templates are over 300 line. Hence being able to put this in an external file and have it cached is a big plus.2) see what I have said to Ryan Oberoi3) yes it is very trivial, the problem with this is the time is the client side page lifecycle when this would occur. If I have an external file like "<script src="template.html" type="text/html"></script>" it will get loaded in parallel with any other external referencess (assuming I put
vdh_ant
all my script references at the bottom of the page). That means if i have all my templates declared as follows "<script src="template.html" type="text/html"></script>" the browser will load these very efficiently. Also if i use $(document).ready(...) none of my JS will run until the templates are loaded. If I go to the loading via ajax, my javascript is halfway through running before it tries to get the template, and I would have to hold until it had gotten the content. Now sure I could try loading all templates first but this complicates things, because then I can't do anything till they,
vdh_ant
are ready. Hence what I was thinking is as follows if I register the templates as follows in the document head <script src="template.html" type="text/html"></script>, then get the same pages via synchronous ajax call, is the browser smart enough to know that it already got the file via the script tag and it doesn't need to go back to the server to get it via ajax???
vdh_ant
+1  A: 

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.

Ryan Oberoi
Ya I did think of this but the problem is the amount of escaping I would have to do. As in if your template is only 5 lines long then doing the escaping, if required isn't too bad. But if it 300 lines plus it becomes a bit harder to maintain. How do you guys get around this?
vdh_ant
I have worked with reasonable sized templates in Ext JS. If you want to encode 300 lines plus, write a simple tool that will escape the HTML text. Plenty of options are available. that will convert template.html to template.js with the html text escaped. A simple method using ruby would be to use CGI.escape(str) where str is read from template.html. This approach may be your best bet.
Ryan Oberoi
Or use a pre-existing tool such as http://search.cpan.org/~rkrimen/Jemplate/lib/Jemplate.pm
David Dorward
In the end I ended up going with this method and I wrote a short script to escape and unescape html content from one text area to another to help make it a little easier. Thanks
vdh_ant
A: 

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.

Michael Siebert
If I put this "<script src="template.html" type="text/html"></script>" in the top of my page and then call template.html via ajax and have it use the cached version that was gotten previously?
vdh_ant
A: 

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);
      }
A: 

No better. Thank you very nice.

Atasözleri