traversing JavaScript namespace

  • Thread starter Plamen Valtchev
  • Start date
P

Plamen Valtchev

This is my problem:

From JavaScript I want to find the list of all defined/loaded
JavaScript functions/objects/names within the current scope (html page
in a browser).

the page could contain many included javascript with the script tag :

<script language="javascript" src="XXX/Script.js"></script>


and I wont to know the functions that have been loaded from the
included script

I couldn find any way of traversing the JavaScript namespace atall !

any ideas ?

Thanks

Plamen
 
M

Martin Honnen

Plamen said:
This is my problem:

From JavaScript I want to find the list of all defined/loaded
JavaScript functions/objects/names within the current scope (html page
in a browser).

the page could contain many included javascript with the script tag :

<script language="javascript" src="XXX/Script.js"></script>


and I wont to know the functions that have been loaded from the
included script

I couldn find any way of traversing the JavaScript namespace atall !


Properties marked as enumerable can be enumerated with
for (var prop in object)
The global object in client side JavaScript is the window object so you
can try
var text = '';
for (var prop in window) {
text += prop + ': ' + window[prop] + '| ';
}
alert(text);
however you will find browser dependant differences as to the properties
that are enumerated.
 
I

Ivo

It 's an interesting idea, using javascript to analyse javascript. The
actual scripts on a page are stored in the DOM under "document.scripts",
with "text" and "src" as the most useful branches.
If it is an inline script, you can break up document.scripts[n].text with a
regular expression:

txt = document.scripts[0].text; // first script tag on the page
reg = /function\s*?(\w+)\s*?\(([,\w\s]*?)\)/gm; // searches function names
if( list = txt.match(reg) )
alert( list.join('\n') );
else
alert( 'No functions in this script!' );

External scripts are a bit more difficult. I have found myself loading
document.srcipts[n].src into a hidden textarea to let the code 'read' the
code. Less clumsy solutions are welcome!
HTH
Ivo
Ira Baxter said:
You could use a real JavaScript parser that understood HTML to figure
this out off line. See
http://www.semdesigns.com/Products/FrontEnds/ECMAScriptFrontEnd.html


--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top