/** * Javascript code to handle adding functions to event listeners of objects. * Includes helper functions to add elements to the body's onload. This is * repeated all over Horde's JavaScript, and this allows for all the details of * maintaining a list of functions to be called to be encapsulated in one * function call. Usage: addObjectEventListener(window, 'load', functionobject); * and addBodyOnload(functionobject); * * Copyright 2005 Matt Warden <mwarden@gmail.com> * * See the enclosed file COPYING for license information (LGPL). If you * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * * * @author Matt Warden <mwarden@gmail.com> */
function addObjectEventListener(obj, event, funct) { // Assign new anonymous function if we're passed a js string // instead of a function reference. if (typeof funct == 'string') { funct = new Function(funct); } if (obj.addEventListener) { obj.addEventListener(event, funct, false); } else if (obj.attachEvent) { obj.attachEvent('on'+event, funct); } else { eval('var old = obj.on'+event+';'); if (old != null) { eval('obj.on'+event+' = function() {' +'old();' +'funct();' +'};'); } else { eval('obj.on'+event+' = funct;'); } // end if } // end if } // end function addListener
function addBodyOnload(funct) { addObjectEventListener(window, 'load', funct); } // end function addBodyOnload
|