PDA

View Full Version : addClickEvent() - add your events without fear


ott
04-26-2006, 03:32 AM
Heres some code to add your events even if there was one+ already.

// function add*Event(e, func)
// www.ottbot.com Richard Maloney
// e - element
// func - function
//
function addClickEvent(e,func) {
if (typeof e.onclick !== 'function') { e.onclick = func; }
else { e.oldclick = e.onclick; e.onclick = function() { this.oldclick(); func();}}}
function addOverEvent(e,func) {
if (typeof e.onmouseover !== 'function') { e.onmouseover = func; }
else { e.oldover = e.onmouseover; e.onmouseover = function() { this.oldover(); func();}}}
function addOutEvent(e,func) {
if (typeof e.onmouseout !== 'function') { e.onmouseout = func; }
else { e.oldout = e.onmouseout; e.onmouseout = function() { this.oldout(); func();}}}

jkd
04-26-2006, 05:29 AM
I wouldn't call it unobtrusive -- it assigns an expando property to the element (oldclick).

If I assign directly to the event handlers, I tend to use:

window.onload = (function(old) {
return function() {
if (old) old.apply(this, arguments);

// the code i want to assign to onload
}
})(window.onload)


And just do that everytime.

ott
04-26-2006, 06:14 AM
Boy I can't figure your function out how it works with existing mouseovers/onloads like mine .:confused:

Regarding your expando point, we could have delete oldboy anytime no?