www.webdeveloper.com
Results 1 to 8 of 8

Thread: Using a string as a function name?

  1. #1
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62

    Using a string as a function name?

    Hi, all,

    I am trying to do something like this:

    Code:
    var functionName = "theFunc";
    functionName ();
    Obviously the above syntax won't work - instead of theFunc() getting called, I get an error.

    Is this possible at all, and if so, what is the correct way to do it?

    All response appreciated!

    // FvW

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Code:
    var functionName = "theFunc";
    window[functionName](); //invokes the function if it is a globally accessible function,
    //which is actually a property of the window object
    //To apply this technique to your own object, is the same:
    var myObject = {
        name : 'fooObj',
        speak : function () {
            alert(this.name);
        }
    };
     
    var funcName = 'speak';
    if (funcName in myObject && typeof myObject[funcName] == 'function') { //myObject has a property by the name of funcName's value, and it is a function
        myObject[funcName]();
    }

  3. #3
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62

    Smile

    Thank you!!!!!!!!!!!! You saved my day... :-)

    // FvW

  4. #4
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Quote Originally Posted by frankvw View Post
    Thank you!!!!!!!!!!!! You saved my day... :-)
    Well, maybe not, after all. Because what I am trying to do still won't work:

    Code:
    function ajaxRead (url, targetObj, updateHandler) {
      var xmlObj = new XMLHttpRequest ();
      xmlObj.onreadystatechange = function () {
        if (xmlObj.readyState == 4) {
          window [updateHandler (ajaxTarget, xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data)];
        }
      }
      xmlObj.open ('GET', url, true);
      xmlObj.send (null);
    }
    
    function ajaxUpdateObj (obj, data) {
      document.getElementById(obj).innerHTML = data;
    }
    When I call the above with something like
    Code:
    ajaxRead (url, target, 'ajaxUpdateObj')
    (having previously set up url and target correctly, of course) I get an error message stating that 'updateHandler is not a function'. Any ideas?

    // FvW

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    You don't have the syntax right
    window [updateHandler (ajaxTarget, xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data)];
    Should be:
    Code:
          window [updateHandler](ajaxTarget, xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data);

  6. #6
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Quote Originally Posted by astupidname View Post
    You don't have the syntax right
    Indeed, that turned out to be the problem. (My Javascript skills are still rather limited.)

    I've got it working now. Onece agains my thanks!!

    // FvW

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    if you made your call ajaxRead (url, target, ajaxUpdateObj), then you can simply say:

    Code:
     updateHandler(ajaxTarget, xmlObj.responseXML.getElementsByTagName('data')[0].firstChild.data);
    there's no reason to pass strings like that; it just slows things down, demands globals, and makes your code harder to maintain.

  8. #8
    Join Date
    Aug 2007
    Posts
    3,767
    Functions are variables like everything else, in this regard, JavaScript is like Lisp. Rnd me shows how to deal with them as variables.
    Great wit and madness are near allied, and fine a line their bounds divide.

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