javascript - jQuery: determine if a function was called as a jQuery event handler (callback) -
i have javascript function can called either directly, either callback jquery event.
in latter case, jquery event object passed argument. when phone call function directly, provide argument (of different kind, still object).
therefore need able check (within function body) how function invoked know sort of argument it's dealing with.
is there simple way that?
accept argument , see if it's instanceof jquery.event:
function yourfunction(e) { if (e instanceof jquery.event) { // ...it event handler phone call } } live example | source
i recommend avoiding sort of thing, though. instead, i'd recommend having event handler call target function (rather using target function actual event handler), if need distinguish behavior depending on how called.
from comment on question:
what tried is: when function called directly, utilize null value in first position , useful argument in sec position.
in case, it's easier:
function yourfunction(e) { if (e) { // ...it event handler phone call -- or @ least, // non-falsey argument of kind passed in } } you don't have pass null function, phone call no arguments. when that, e undefined, falsey. (the event object jquery passes handler never falsey.)
javascript jquery event-handling
No comments:
Post a Comment