Ticket #654 (closed enhancement: fixed)
reduce useless test in event handlers
| Reported by: | mokhet | Owned by: | gogo |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Xinha Core | Version: | trunk |
| Severity: | normal | Keywords: | event |
| Cc: |
Description
HTMLArea._addEvent = function(el, evname, func) {
if (HTMLArea.is_ie) {
el.attachEvent("on" + evname, func);
} else {
el.addEventListener(evname, func, true);
}
HTMLArea._eventFlushers.push([el, evname, func]);
};
In this piece of code, we can see the test (HTMLArea.is_ie) is run at every event added.
I think it would be better to test this once and create a different function for each handled case. This is not a lot of save you can think, but hey, why not avoid some tests when we can easily avoid them.
if (HTMLArea.is_ie) {
HTMLArea._addEvent = function(el, evname, func) {
el.attachEvent("on" + evname, func);
HTMLArea._eventFlushers.push([el, evname, func]);
};
} else {
HTMLArea._addEvent = function(el, evname, func) {
el.addEventListener(evname, func, true);
HTMLArea._eventFlushers.push([el, evname, func]);
};
}
But i dont like the use of HTMLArea.is_ie, it is very dirty IMO. So i propose this change :
if (document.addEventListener) {
HTMLArea._addEvent = function(el, evname, func) {
el.addEventListener(evname, func, true);
HTMLArea._eventFlushers.push([el, evname, func]);
};
} else if (document.attachEvent) {
HTMLArea._addEvent = function(el, evname, func) {
el.attachEvent("on" + evname, func);
HTMLArea._eventFlushers.push([el, evname, func]);
};
} else {
HTMLArea._addEvent = function(el, evname, func) {
alert('_addEvent is not supported');
};
}
The same concept can be used for removeEvent and stopEvent
if (document.addEventListener) {
HTMLArea._addEvent = function(...
HTMLArea._removeEvent = function(...
HTMLArea._stopEvent = function(...
} else if (document.attachEvent) {
HTMLArea._addEvent = function(...
HTMLArea._removeEvent = function(...
HTMLArea._stopEvent = function(...
} else {
HTMLArea._addEvent = function(...
HTMLArea._removeEvent = function(...
HTMLArea._stopEvent = function(...
}
I will upload a diff patch in a moment.
Attachments
Change History
Note: See
TracTickets for help on using
tickets.
