Advanced Search

Results 1 to 6 of 6

Thread: HELP !!! Store & execute function from array (stack) ?

  1. #1
    Join Date
    Aug 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP !!! Store & execute function from array (stack) ?

    Hello,

    I need to attach user function to my global javascript.

    My global javascript validate each & evey controls via DOM traversing.

    I want ,User also attach functions like compareEmail etc to my JavaScript

    So i m trying to store userfuction through register(Function) procedure.

    Whenever user click Submit button, that user procedure will be called first & then my own dom traversing procedure will be start. But , after storing the function into userFunctionstack, i cant able to execute via by just calling
    for i = 0 to length(userFunctionstack)
    userFunction[i]
    end for

    Any one have solution of this prob, how to store/exec JavaScript function from Array ???

    Plz reply...

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

    Default

    The code to execute every function in an array of functions is
    Code:
    for(var i  0; i < userFunctionstack.length; ++i)
      userFunctionstack[i]();
    but I'm not entirely sure about this register() stuff.
    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!

  3. #3
    Join Date
    Aug 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks

    Yes... it's works... Thanks very much for your reply

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The code to execute every function in an array of functions is
    Code:
    for(var i 0; i < userFunctionstack.length; ++i) userFunctionstack[i]();
    but I'm not entirely sure about this register() stuff.
    Twey, are you sure that it works? I am asking this because I have tried that several times but unfortunately I could never get it to work.

    Well if it does work then is this the right way to use it??

    Code:
    function userFunctionstack1(){
    alert('user function 1');
    }
    function userFunctionstack2(){
    alert('user function 2');
    }
    function userFunctionstack3(){
    alert('user function 3');
    }
    function userFunctionstack4(){
    alert('user function 4');
    }
    for(var i 0; i < userFunctionstack.length; ++i) userFunctionstack[i]();

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

    Default

    Code:
    var userFunctionstack = [
      function() {
        alert("user function 1");
      },
      function() {
        alert("user function 2");
      },
      function() {
        alert("user function 3");
      },
      function() {
        alert("user function 4");
      }
    ];
    
    for(var i = 0; i < userFunctionstack.length; ++i)
      userFunctionstack[i]();


    That's the "neat" way of doing it. We can do it with plain functions like yours too (as opposed to an array of functions, which was what my code was for):
    Code:
    for(var i = 1; typeof window['userFunctionstack' + i] === 'function'; ++i)
      window['userFunctionstack' + i]();
    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!

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah!!! That's the way. Thanks for your help. I was trying to make a progress bar(with AJAX and PHP) but could not make it to work as the method I used didn't work. Thanks a lot!!!

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
  •