admin管理员组

文章数量:1194107

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.

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.

Share Improve this question edited Nov 24, 2022 at 19:28 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 29, 2012 at 19:19 JonJon 2,1185 gold badges25 silver badges51 bronze badges 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 Swangren Commented 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... – Jon Commented 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. – Blazemonger Commented 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 Commented Mar 29, 2012 at 19:27
Add a comment  | 

2 Answers 2

Reset to default 17

Use the type property in the event object:

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

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);
}

本文标签: javascriptCheck which event fired the function with jQueryStack Overflow