prevent [removed] being overridden javascript

后端 未结 3 1509
青春惊慌失措
青春惊慌失措 2020-12-18 19:40

How to overcome event handlers being overridden? I have a script say a.js

window.onload = function () {
   //Handler in a.js
}

Another scri

相关标签:
3条回答
  • 2020-12-18 20:01

    you should use addEventListener() to have various handlers for the same event

    window.addEventListener("load", yourfunction, false); 
    
    0 讨论(0)
  • 2020-12-18 20:12

    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.

    0 讨论(0)
  • 2020-12-18 20:13

    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()}
    
    0 讨论(0)
提交回复
热议问题