16

How can I check which event called my function on JavaScript with jQuery?

Like I have:

var mycall= function() { 
    alert('Which Witch is Which?');
}

$(window).load(mycall);
$(window).resize(mycall);

I understand that I could pass a parameter to the function, still I'm interested in a way to do this without passing any parameters.

4
  • 2
    "I understand that i could pass a parameter to the function, still i'm interested in a way to do this without passing any parameters." - And why would that be? So you know the solution, but you want a more complicated way to achieve the same thing? Seems backwards to me, go with simple, well understood methods to accomplish simple, well understood problems.
    – Ed S.
    Mar 29, 2012 at 19:21
  • 2
    let's say i have a code which i use in 15 websites, i'd rather change only one JS then all the calls i made to that JS :) also out of pure curiosity... Mar 29, 2012 at 19:22
  • @EdS. He just wants to know if there is some kind of analog to event.target. I don't know of one, but I admit it would be a nice thing to have. Mar 29, 2012 at 19:24
  • 1
    What you call "pass a parameter" is actually "declare named parametr". Actual parametrs are passed by calling code (jQuery), so you'll have params anyway. They could be accessed w/o declaration using arguments like I wrote.
    – kirilloid
    Mar 29, 2012 at 19:27

2 Answers 2

17

Use the type property in the event object:

var mycall = function(e) { 
  alert(e.type);
};
2
  • 6
    @SKS and he even wrote an extra space between mycall=
    – ajax333221
    Mar 29, 2012 at 19:27
  • 3
    @ajax333221 but have 2 less spaces before alert. So they type with 1/39 char per sec :-D
    – kirilloid
    Mar 29, 2012 at 19:28
6

Add an event to the function args and then event.type will give tell which event is triggered. See below,

var mycall= function(e) { 
    alert(e.type);
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.