SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Norway - Oslo
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FInd defined functions

    Hi.

    I want to find all functions that are named /init[a-zA-Z]*/ and have them run on page load. As far as I can see I have two options, the first being to find all these functions somehow and run them, the second is to register the functions somewhere when i write a new init-function.
    I havent been able to find a way to list all (user) defined functions in scope, does anyone know of a way to do this?

    Thanks in advance.
    Raymond Julin
    Developer: Hardware.no, Amobil.no, Gamer.no, Prisguide.no ...
    Owner: Kulturo.no

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Findus
    I havent been able to find a way to list all (user) defined functions in scope, does anyone know of a way to do this?
    Depends on which scope. Member functions (of some object) are enumerable via for...in loop. Global functions are members of window object, however in Explorer they are not enumerable, that is there is no way to list them except to parse source code. Local functions are not accessible via reflection.

    Why don't you use attachEvent/addEventListener?

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not easy in IE to retrieve all the window scope functions by name,
    but this page does it in any modern browser(and exposes the code):
    http://www.yankeeweb.com/_shadow/ghost_fun.html

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good job mrhoo, mind sharing your approach?

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I want to work with the methods in my code- to see who is calling who, or what methods should get assigned to which objects, I start by creating a Fun object, that begins life by reading the
    Code:
    document.getElementsByTagName('script') collection
    Fun uses the src attributes of the script elements for file paths in ajax calls that feed the text responses to a parsing function.
    Since I am only loading the scripts already in the document, these files process quickly, from the top of the cache.

    I parse each using a regular expression exec loop, line by line,
    to extract function names as strings. This builds the members of Fun, named with the file names(minus path & extension) and each consisting of an array of the function names (as strings), which can be used return the functions, or their string representations.
    Last edited by mrhoo; Nov 25, 2006 at 23:22.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, thanks.


Bookmarks

Posting Permissions

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