If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

capture functions in Javascript

Hello, I wanted to know whether there is a way for me to capture functions that have been defined in a Javascript file.
It is for testing purposes. I am actually creating a web page where the user would be asked to upload a javascript file and then the list of functions would appear in a drop down list or something for the user to select upon which function he/she wants to perform the test.

Everything in a web page is an object or property of either window or document.

You would need to traverse the DOM tree to find these objects, a properly written script will have its scripts or calls in the <head> of the document which narrows it down a bit for you, other plonk their scripts in the <body>

So if you look in the branches of the DOM tree for <head> and <body>, you will find the scripts and then you will be able to read the functions (except those that are written in native javascript, as in part of javascript itself)

STOP using $ prefix on JavaScript variable names...
Please remember to wrap any code you have in forum tags:- [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]If you can't think outside the box, you will be trapped forever with no escape...

...also there's a global object. In addition to its common properties (properties defined by JavaScript language and properties of a browser) the global object contains properties defined by a user program. So you may write a function to query these properties...

...also there's a global object. In addition to its common properties (properties defined by JavaScript language and properties of a browser) the global object contains properties defined by a user program. So you may write a function to query these properties...

Even if "x" in the example above is given global scope, a simple scanning for type won't disclose the existence of its "talk" method/function. You can set it up to detect these as well but its going to take a much more complicated search to reveal these.

the use of .toString or .valueOf is great in circumstances where you want to read the function as a string, then you can parse the string for instance of methods.

STOP using $ prefix on JavaScript variable names...
Please remember to wrap any code you have in forum tags:- [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]If you can't think outside the box, you will be trapped forever with no escape...

Right now I can just think of something like... Write a function that will collect all the properties of the Global object whose value is a function. Invoke the function at the very beginning of a web document and call it again at the very end of a web document. Then write a function that will compare the results and return user defined functions if any.

Even if "x" in the example above is given global scope, a simple scanning for type won't disclose the existence of its "talk" method/function. You can set it up to detect these as well but its going to take a much more complicated search to reveal these.

Well...that's true but I don't think that so complicated search would have to be performed. In your example, you could extend your search to filter not just functions but also objects. Then you can loop through its properties and search for functions and yet if its property value is another object you can loop through its properties and search if there's any function etc...