kael.shipman Posted June 18, 2007 Share Posted June 18, 2007 Hey, Does anyone know how to pass arguments to event handler functions like 'window.onload = doSomething;'? Obviously in this case you can't because the onload handler needs the function address and not the result (which would be returned if we put parentheses and arguments in), but I find it very inconvenient that such a limitation exists and I feel like there's got to be some easy construct out there that I've just never heard of before that everyone else uses. Any advice? Thanks, Kael Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 18, 2007 Share Posted June 18, 2007 The only way I've seen it done is by creating a wrapper function which would pass the arguments: function funcWithArgs(arg1,arg2){ ... } function wrapperFunc(){ funcWithArgs(a1,a2); } .... window.onload = wrapperFunc; Quote Link to comment Share on other sites More sharing options...
kael.shipman Posted June 19, 2007 Author Share Posted June 19, 2007 hm.... Yeah, that's roughly what I came up with (though it may be easier to say window.onload = function() { funcWithArgs(a1,a2) }), but then that still doesn't address the issue of actual variable arguments. I suppose I could always make them global variables or something, but that seems like a lot of clutter. If that seems confusing, here's what I mean: ... function doSomething(cha,fah) { var scrpt = createNewScriptElement([scriptSource]); scrpt.onload = doSomethingElse; } ... In this case, I'd like to pass variables cha and fah to the function "doSomethingElse," but the only way to do so is to make them global variables and pick them up from inside doSomethingElse. Anyway, if that's the way it's gotta be then I'll live, but if anyone has any other ideas, let me know! -kael Quote Link to comment Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 Yeah... but how would you pass the variables before if they weren't global? Just out of curiosity. I mean if you were able to do: window.onLoad = function(a,b) { ... } Just wondering . I'm not sure what you are trying to accomplish so hard for me to figure out a solution (if there is any). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.