Results 1 to 5 of 5

Thread: Treat string as a function

  1. #1
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Treat string as a function

    Is there anyway to treat a string as a function name? To make something like this work(it doesn't)
    Code:
    somestring = 'function_name';
    window.onload = somestring
    function function_name(){
    }

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    I think JavaScript doesn't allow any such feature like PHP

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Not quite. However, when you define a global function or variable, it becomes a property of the global object. From here, you can access it by its name using the ECMAScript objects-as-associative-arrays idea:
    Code:
    var somestring = 'function_name';
    window.onload = window[somestring];
    function function_name(){}
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thanks Twey! Exactly what I was looking for.

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    hmmm thats a new thing for me thanks for the info Twey

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
  •