niks wrote:
I'm a javascript novice trying to write a client-side program that
needs to examine the text of some included code.
Suppose you have the following snippet in an html document:
<script id="included" src="inc.js" type="text/javascript"></script>
I want to be able to write a function along the following lines:
function inspect() {
var s = document.findByElementId("included");
var includedCodeAsString = ... ; //<------- s.nodeValue,
s.innerHTML???
//analyze includedCodeAsString
}
That is, I want to be able to access the file inc.js which should be
have been downloaded by the browser. I've tried things like s.innerHTML
without luck.
Probably because there is no innerHTML (there is actually no content at
all). What you are trying to get is the content of the resource
identified by the script element's src attribute.
My current solution to this is to use AJAX (XMLHttpRequest) to GET
inc.js from the server ... but this feels wrong.
That is probably the the best way, though it has all the usual AJAX
caveats. You can try loading the script into a hidden iFrame element,
then grab it from there, but I get security warnings from IE and it
won't let me see the file (I tried a resonably complex bit of script).
Firefox seems happy. You won't be able to do it if the script is from
another domain.
You will also get issues with the iFrame method where characters in the
script are interpreted as HTML - particularly "<" being taken as the
start of an opening tag. I would characterise it as extremely
unreliable.
I would have to question why you are downloading a script from your own
server, then trying to process it using another script - are you trying
to mess with someone eles's script?
--
Rob