473,242 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 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 2118

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: aa | last post by:
I have an UTF-8 PHP file which generates HTML Then I cut the beginning of that file, pasted into another UTF-8 file ind included this second file into the first one. After that the whole page...
19
by: aa | last post by:
Is a PHP variable supposed to be seen in a .js file included into a .php file? I have a client side javascript code stored in a .js file which is included into a PHP file using <script...
0
by: Kookymon1 | last post by:
This is an attempt to respond to an older question (several months). Date: 2002-03-07 13:10:23 PST Subject: On the Common DOM API and Applets. The original message was: >LiveConnect and the...
2
by: Generic Usenet Account | last post by:
I am trying to create a Java application that reads a list of URLs from a file and stores their contents on the local file system. I have succeeded in accessing normal websites, but I am unable to...
0
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: Noy B | last post by:
Hi, I have developed a small application that is using a MSAccess DB. the problem is that it was developed on a machine where the application and the DB are both located. now it needs to be...
4
by: Madmartigan | last post by:
Hello I have attempted the source code from Chapter 4 in Accelerated C++ page 70 but keep getting the above error. I'm not sure whether it is in the way I have saved the header files or because my...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.