473,513 Members | 8,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2125

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
1895
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
2227
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
5584
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
3571
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
2242
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
4970
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
12055
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
4199
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
7380
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
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.