Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Browser Event object normalization?

I'm looking for a good resource on event normalization on the event object. I'm trying to do it myself but I keep feeling like I'm going to miss something.

Here's what I have so far, tell me if I missed anything.

var eFix = function(e) {
    e = e || window.event;
    e.target = e.target || e.srcElement;
    e.offsetX = e.offsetX || e.layerX;
    e.offsetY = e.offsetY || e.layerY;
    e.relatedTarget = e.relatedTarget ||
        e.type == 'mouseover' ? e.fromElement : e.toElement;
    e.target = e.target || e.srcElement;
    if (target.nodeType === 3) target = target.parentNode; //Safari bug
    return e;
};

Has anyone seen a complete normalization function? Did I miss anything? (Needless to say we're going for W3C model not IE)

like image 254
qwertymk Avatar asked Jan 10 '11 02:01

qwertymk


1 Answers

There is another problem with your code:

e.layerX only works on positioned elements, so at the very least you need to add a "position:relative" to your element to function. Secondly e.offsetX only works correctly in IE8 and later, so you should probably refrain from using it either way (although I am using them right now, but this only needs to work in specific browsers).

like image 89
David Mulder Avatar answered Sep 30 '22 13:09

David Mulder