471,596 Members | 918 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Accessing an included source file as a string

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

Aug 9 '06 #1
3 2069

niks wrote:
<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.

Aug 9 '06 #2

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

Aug 10 '06 #3
RobG wrote:
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.
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.

Aug 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kookymon1 | last post: by
2 posts views Thread by Generic Usenet Account | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.