properly bind javascript events

后端 未结 4 1546
无人共我
无人共我 2020-12-05 14:47

I am looking for the most proper and efficient way to bind javascript events; particularly the onload event (I would like the event to occur after b

4条回答
  •  余生分开走
    2020-12-05 15:23

    There are two different ways to do it. Only one will work; which one depends on the browser. Here's a utility method that uses both:

    function bindEvent(element, type, handler) {
       if(element.addEventListener) {
          element.addEventListener(type, handler, false);
       } else {
          element.attachEvent('on'+type, handler);
       }
    }
    

    In your case:

    bindEvent(window, 'load', function() {
        // all elements such as images are loaded here
    });
    

提交回复
热议问题