473,385 Members | 1,325 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,385 software developers and data experts.

Get contents of included JS file from within JS?

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
4 3443
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


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

Similar topics

4
by: Deane Barker | last post by:
I have a function that selects a file to include, then includes is. The file is including within the function, like so: function include_file($file_name) { require $file_name; return; }
2
by: Jon Pope | last post by:
Is it possible to view the contents of a CHM file within IE? I've attempted to do this by right-clicking on a CHM file then selecting "Open With..." and finally selecting "IE", but nothing...
7
by: TLM | last post by:
I am trying to build a web application that will contain links to files on a users local computer. I am assuming that the files will be in a known location and can display in a browser window. ...
6
by: Andy B | last post by:
What would i use to look for changes in a file every minute?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.