Accessing an included source file as a string

N

niks

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.

My current solution to this is to use AJAX (XMLHttpRequest) to GET
inc.js from the server ... but this feels wrong.

Thanks for any help you can provide.
-Nik
 
W

web.dev

niks said:
<script id="included" src="inc.js" type="text/javascript"></script>

The script element does not have an id attribute.
var s = document.findByElementId("included");

The method name is incorrect, it should be the following:

var s = document.getElementById("included");

But, the script element does not have an id attribute as said above,
therefore your statement will still not do what you wanted to do.
My current solution to this is to use AJAX (XMLHttpRequest) to GET
inc.js from the server ... but this feels wrong.

Your solution using AJAX sounds reasonable.
 
R

RobG

niks said:
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?
 
N

niks

RobG said:
That is probably the the best way, though it has all the usual AJAX
caveats.

That's too bad. It seems like an unnecessary round-trip to the server
to fetch something that the browser should already have. Anyway ... I
just wanted to make sure that I wasn't missing some obvious way of
doing this without AJAX. Thanks very much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top