How to overcome event handlers being overridden? I have a script say a.js
window.onload = function () {
//Handler in a.js
}
Another scri
you should use addEventListener() to have various handlers for the same event
window.addEventListener("load", yourfunction, false);
Use element.addEventListener
or window.attachEvent
in down-level IE versions.
Sample addEvent
method:
function addEvent(node, type, listener) {
if (node.addEventListener) {
node.addEventListener(type, listener, false);
return true;
} else if (node.attachEvent) {
node['e' + type + listener] = listener;
node[type + listener] = function() {
node['e' + type + listener](window.event);
}
node.attachEvent('on' + type, node[type + listener]);
return true;
}
return false;
};
Note - Most, if not all, modern JavaScript libraries like jQuery
and MooTools
have their own implementations. I recommend leveraging their API's - as they abstract out different browser implementations and have been thoroughly tested.
Yes, the second statement will override the first one. Because you are using the traditional registration model only one event handle is allowed for any given event and object. The function assignment are not cumulative. But you can:
window.onload = function () {handlerinb(); handlerina()}