Jump to content

Passing arguments to event handler functions


kael.shipman

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.