www.webdeveloper.com
Results 1 to 9 of 9

Thread: capture functions in Javascript

  1. #1
    Join Date
    Sep 2013
    Posts
    3

    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.

    Thanking you in advance

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,519
    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)
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    ...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...

    Something like this:

    Code:
    function getGlobFunctions() {
        var globFuncs = [];
    
        for (var p in this) 
            if (typeof this[p] === "function") globFuncs.push(p);
    
        return globFuncs;
    }
    
    var globFuncs = getGlobFunctions();
    
    document.write(globFuncs.join("<br />"));

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,444
    Quote Originally Posted by tech_soul8 View Post
    ...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...

    Something like this:

    Code:
    function getGlobFunctions() {
        var globFuncs = [];
    
        for (var p in this) 
            if (typeof this[p] === "function") globFuncs.push(p);
    
        return globFuncs;
    }
    
    var globFuncs = getGlobFunctions();
    
    document.write(globFuncs.join("<br />"));
    How do you determine which functions are JS vs browser vs user?

  5. #5
    Join Date
    Mar 2009
    Posts
    523
    There is also the problem that a lot of the "functions" one can encounter are actually "methods" of javascript objects. A lot of libraries do this. For example:

    Code:
    var x = function(){
            var q = {};
            q.talk = function(){ alert("HI THERE");}
            return q;
        }();
    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.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,519
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by JMRKER View Post
    How do you determine which functions are JS vs browser vs user?
    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.

    It's a kinda sloppy coding but just to show you what I meant.

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
        <title>Untitled</title>
        <script type='text/javascript'>
            var glFncs;
            
            function getGlobalFunctions() {
                var gloFuncs = [];
                
                for (var p in this) {
                    if (typeof this[p] === "function") gloFuncs.push(p);
                }
                
                return gloFuncs;
            }
            
             function getUserFunctions() {
                var glFncs2, usrFncs = [];
                
                if (getGlobalFunctions().length != glFncs.length) {
                    glFncs2 = getGlobalFunctions();
                    
                    for (var i = 0; i <= glFncs2.length; i++) {
                        
                        for (var j = 0; j <= glFncs.length; j++) {
                            if (glFncs2[i] === glFncs[j]) break;
                            else if (j === glFncs.length) usrFncs.push(glFncs2[i]);
                        }
                    }
                }
                
                return (usrFncs.length > 0) ? usrFncs.toString() : "There is no any user defined function!!";
            }
            
            glFncs = getGlobalFunctions();
        </script>
    </head>
    <body>
        <script type="text/javascript">
            function test() {};
            function test2() {};
        </script>
        <script type="text/javascript">
            console.log(getUserFunctions());
        </script>
    </body>
    </html>
    I don't know if it's a bulletproof but like a said right now I cannot think of any other solution to this problem. :P

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by Tcobb View Post
    There is also the problem that a lot of the "functions" one can encounter are actually "methods" of javascript objects. A lot of libraries do this. For example:

    Code:
    var x = function(){
            var q = {};
            q.talk = function(){ alert("HI THERE");}
            return q;
        }();
    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...

    Feel free to correct me if I'm wrong.

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by \\.\ View Post
    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.
    Yeah, didn't cross my mind. It would be nice and easy to perform that kind of search.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles