PDA

View Full Version : HELP !!! Store & execute function from array (stack) ?



vikasbhagwagar
08-24-2006, 05:07 PM
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...

Twey
08-24-2006, 05:27 PM
The code to execute every function in an array of functions is
for(var i 0; i < userFunctionstack.length; ++i)
userFunctionstack[i]();but I'm not entirely sure about this register() stuff.

vikasbhagwagar
08-25-2006, 11:29 AM
Yes... it's works... Thanks very much for your reply :)

shachi
08-26-2006, 06:55 AM
The code to execute every function in an array of functions is


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??



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]();

Twey
08-26-2006, 07:01 AM
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):
for(var i = 1; typeof window['userFunctionstack' + i] === 'function'; ++i)
window['userFunctionstack' + i]();

shachi
08-26-2006, 12:26 PM
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!!!:)