I have my own custom event function for adding/removing listeners. When the script is executed in IE, the add function looks something like this:
function add(element, eventType, method) {
var method2 = function () {
method.call(element, window.event);
}
element.attachEvent("on" + eventType, method2);
}
(Some stuff has been left out for simplicity.) Now what I want to do is come up with a cross-browser way of stopping the default action. After some messing around, here’s what I got to work:
function add(element, eventType, method) {
var method2 = function () {
var e = window.event;
e.preventDefault = function () {
this.returnValue = false;
};
method.call(element, e);
};
element.attachEvent("on" + eventType, method2);
}
(I know that I’m going closure crazy here; that can be simplified later.) This way, regardless of browser, I can just call preventDefault(). My only problem is that I have to do this every time an event is added. Is there no way to edit the event object once, ahead of time? Maybe a prototype somewhere?