Is there a way to take the current target of an event with IE 7 or 8?
With other browser (firefox, opera, chrome etc.) we can use
event.currentTarget
or
This function creates currentTarget
in case it is IE, so you no longer need to patch your code!
function eventListener(e,t,f) {
if(e.addEventListener)
e.addEventListener(t,f);
else
e.attachEvent('on' + t,
function(a){
a.currentTarget = e;
f(a);
});
}
Regular JS(will not work on IE below 9):
function myfunction(e) {
alert(e.currentTarget.id);
}
e = document.getElementById('id');
e.AddEventListener(e,'mouseover',myfunction);
With this function(will work on IE below 9):
function myfunction(e) {
alert(e.currentTarget.id);
}
e = document.getElementById('id');
eventListener(e,'mouseover',myfunction);
Internet Explorer 6 - 8 do not implement event.currentTarget.
Quote from Mozilla Developer Network:
On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard element.attachEvent method. In this model, there is no equivalent to event.currentTarget and this is the global object. One solution to emulate the event.currentTarget feature is to wrap your handler in a function calling the handler using Function.prototype.call with the element as a first argument. This way, this will be the expected value.
If you use jQuery anyway, then jQuery takes care of normalizing the event object and its properties (as stated numerous times in the other answers). Quote from jQuery API docs:
jQuery’s event system normalizes the event object according to W3C standards.
I had similar problem. I solved it using keyword this
as stated in an article on brainjar.com
To get the equivalent of the currentTarget property in IE, use the this keyword as an argument when setting the event handler in a tag.
...
function myHandler(event, link) { ... }
On the same page you can find the following table :
With Prototype JS you can use :
var target = Event.element(event);
I'd like to give a more simple answer, the meat of this is the same as the meat in anas' and user1515360's answers:
if (browserDoesNotUnderstandCurrentTarget) {
someElement.onclick = function (e) {
if (!e) { var e = window.event; }
e.currentTarget = someElement;
yourCallback(e);
}
}
else {
someElement.onclick = yourCallback;
}
Substitute onclick for whatever event you wish of course.
This makes e.currentTarget available on browsers that do not support currentTarget.
with this function you can pass the object when adding and get it in the listener. the problem about this is that you have an anonymous function as eventlistener and in actionscript you cannot remove an anonymous listener. dunno bout js.
addEvent:function(object,type,listener,param)
{
if(object.addEventListener)
object.addEventListener(type, function(e){ listener(object, e, param);}, false );
else
if(object.attachEvent)
object.attachEvent('on'+type, function(e){ e = getEvent(e); listener(object, e, param);});
},
getEvent:function(e)
{
if(!e) e = window.event; // || event
if(e.srcElement) e.target = e.srcElement;
return e;
},
removeEvent:function(object,type,listener)
{
if(object.removeEventListener)
object.removeEventListener(type, listener, false);
else
object.detachEvent('on'+type, listener);
}
var div = document.getElementById('noobsafediv');
var div2 = document.getElementById('noobsafediv2');
addEvent(div,'mouseover',mouseover,['astring',111,div2]);
function mouseover(object,e,param)
{
console.log(object,e,param);
}
its my framework and i call it jNoob.