eventsManager_JS

    1| /*
    2| Project: eventsManager.js
    3| Version: 1.0
    4| Name: Keith Gallistel
    5| 
    6| Using "9.1 Equalizing the IE and W3c Event Models" on pp. 236-40 and "9.3 Appending Mulitiple Load Event Handlers" on pp.242-3 of JavaScript & DHTML Cookbook (2nd edition).
    7| 
    8| (http://www.amazon.com/gp/product/0596514085?ie=UTF8&tag=keithgallcom-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596514085)
    9| 
  10| Change log 1.0:
  11| Date: 2012-08-28
  12| -First release.
  13| */
  14| 
  15| function addEvent(elem, evtType, func, capture){
  16|     capture = (capture) ? capture : false;
  17|     if(elem.addEventListener){
  18|         elem.addEventListener(evtType, func, capture);
  19|     }
  20|     else if(elem.attachEvent){
  21|         elem.attachEvent("on" + evtType, func);
  22|     }
  23|     else{
  24|         //for IE/Mac, NN4, and older
  25|         elem["on" + evtType] = func;
  26|     }
  27| }
  28| 
  29| function removeEvent(elem, evtType, func, capture){
  30|     capture = (capture) ? capture : false;
  31|     if(elem.removeEventListener){
  32|         elem.removeEventListener(evtType, func, capture);
  33|     }else if(elem.detachEvent){
  34|         elem.detachEvent("on" + evtType, func);
  35|     }else{
  36|         //for IE/Mac, NN4, and older
  37|         elem["on" + evtType] = null;
  38|     }
  39| }
  40| 
  41| function addOnLoadEvent(func){
  42|     if(window.addEventListener || window.attachEvent){
  43|         addEvent(window, "load", func, false);
  44|     }
  45|     else{
  46|         var oldQueue = (window.onload) ? window.onload : function(){};
  47|         window.onload = function(){
  48|             oldQueue();
  49|             func();
  50|         }
  51|     }
  52| }