By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Get contents of included JS file from within JS?

P: n/a
Let me break down the issue:

Ultimate Goal: Building a custom debugger window which shows a
function trace in and out of functions as they are called, which can
be executed via IE's Context Menu and used on any page (no source
changes needed, and all code can be IE-specific)

Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.

Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.

QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?

RELATED QUESTION: If not this, then is there any way (can be
IE6-specific) to access all the user-defined functions in a page? They
don't show up when you iterate the window[] namespace, yet they DO
exist there.

My debug windows is working, and my code currently goes in and renames
functions and adds debug message hooks. But it's useless if I can't
get access to the functions included in external files!

Thanks!

Matt Kruse
Javascript Toolbox: http://www.JavascriptToolbox.com/
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <7a*************************@posting.google.com> ,
ma**@mattkruse.com enlightened us with...

My debug windows is working, and my code currently goes in and renames
functions and adds debug message hooks. But it's useless if I can't
get access to the functions included in external files!


Doesn't Mozilla have an open source javascript debugger? Try looking at
their source. Maybe it will help.

Other than that, I'm not nearly good enough to answer this one. :)

--
--
~kaeli~
Why did kamikaze pilots wear helmets?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2

P: n/a


Matt Kruse wrote:
Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.

Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.

QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?


IE on Win has Msxml.XMLHTTP to make HTTP requests, you could use that to
read the source of a file:

<html>
<head>
<title>document.scripts</title>
<script type="text/javascript" src="test20031223.js"></script>
<script type="text/javascript">
function getScriptFileContent (src) {
var httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
httpRequest.open('GET', src, false);
// the false means synchronous loading but assuming the file
// is already cached it shouldn't do much harm
httpRequest.send(null);
return httpRequest.responseText;
}

function checkScripts () {
for (var i = 0; document.scripts && i < document.scripts.length; i++) {
var script = document.scripts[i];
if (script.text) {
alert(script.text);
}
else {
alert(getScriptFileContent(script.src));
}
}
}

window.onload = checkScripts;
</script>
</head>
<body>
<p>
document.scripts test
</p>
</body>
</html>

However be warned that the use of responseText is limited, if the .js
file is not UTF-8 encoded the and contains non-ASCII characters they
show up garbled in responseText

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3

P: n/a
In article <7a*************************@posting.google.com> , ma**@mattkruse.com
(Matt Kruse) writes:

<--snip-->
Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.


Look into an HTTP Request to get the contents of any external file loaded:

<URL: http://jibbering.com/2002/4/httprequest.html />

You could parse the document.documentElement.innerHTML to find all the script
tags that have src= attributes, and go from there to get a list of all the
external files loaded, and then do HTTPRequests to read them all.

In testing it with IE6.0, it freely reads any file even if its not on the
server it originated from.
--
Randy
Jul 20 '05 #4

P: n/a
Matt Kruse wrote:
Ultimate Goal: Building a custom debugger window which shows a
function trace in and out of functions as they are called, which can
be executed via IE's Context Menu and used on any page (no source
changes needed, and all code can be IE-specific)
There is a JScript debugger and Visual Studio (.NET) for the IE browser
component. (There is Venkman for Mozilla/5.0.)
Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.
You could parse the whole document.documentElement.innerHTML but what
would that achieve?
Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.
You can make it the src of a HTMLIFrameElement or HTMLObjectElement and
access its properties.
QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?
I know none.
RELATED QUESTION: If not this, then is there any way (can be
IE6-specific) to access all the user-defined functions in a page?
See above.
They don't show up when you iterate the window[] namespace,


That depends on the UA. They show up in Mozilla/5.0.
PointedEars
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.