Ie8 Onclick Handler Event
I've read most of the relevant articles in quirksmode.org but still I'm not sure about this one: Making my app compatible with IE8 (fun fun fun) I encounter this issue when trying
Solution 1:
Yes, the event object is not passed as a parameter to DOM0-style event handlers in IE <= 8. You need to get it from window.event
instead. If you add a parameter called event
, in IE <= 8 this will be undefined
and references to event
within the event handler will resolve to the undefined parameter rather than window.event
. I usually use window.event
to make this explicit in the code:
link.onclick = function(evt) {
evt = evt || window.event;
}
Solution 2:
Once I needed to handle click events on window and work with target
property in IE8
The solution that I used in my project:
document.onclick = function(event) {
event = event || fixEvent.call(this, window.event);
var target = event.target || event.srcElement;
var className = target.className;
// ....
};
//For IE8 // source: https://learn.javascript.ru/fixeventfunctionfixEvent(e) {
e.currentTarget = this;
e.target = e.srcElement;
if (e.type == 'mouseover' || e.type == 'mouseenter') e.relatedTarget = e.fromElement;
if (e.type == 'mouseout' || e.type == 'mouseleave') e.relatedTarget = e.toElement;
if (e.pageX == null && e.clientX != null) {
var html = document.documentElement;
var body = document.body;
e.pageX = e.clientX + (html.scrollLeft || body && body.scrollLeft || 0);
e.pageX -= html.clientLeft || 0;
e.pageY = e.clientY + (html.scrollTop || body && body.scrollTop || 0);
e.pageY -= html.clientTop || 0;
}
if (!e.which && e.button) {
e.which = e.button & 1 ? 1 : (e.button & 2 ? 3 : (e.button & 4 ? 2 : 0));
}
return e;
}
Post a Comment for "Ie8 Onclick Handler Event"