/ Published in: JavaScript
This lets you attach a new onload event without overwriting any previous events.
Expand |
Embed | Plain Text
function addLoadEvent(func) { var oldonload = window.onload; if(typeof window.onload != "function") window.onload = func; else window.onload = function() { oldonload(); func(); } }
Comments
Subscribe to comments
You need to login to post a comment.

That's not a very robust implementation... I think it should be more like this:
function chain(object, methodName, newMethod){ if(object && typeof object == 'object' && methodName && typeof methodName == 'string' && newMethod && typeof newMethod == 'function'){ var old = object[methodName]; if(old && typeof old == 'function'){ var oldArgs = []; var newArgs = []; for(var i0 = 0; i0 < old.length; i0++){ oldArgs[i] = 'arg' + i0; } for(var i0 = 0; i0 < newMethod.length; i0++){ newArgs[i] = 'arg' + i0; } oldArgs = oldArgs.join(', '); newArgs = newArgs.join(', '); var args = old.length > newMethod.length ? oldArgs : newArgs; object[methodName] = eval('function(' + args + '){\n' + ' old.call(' + oldArgs + ');\n' + ' newMethod.call(' + newArgs + ');\n' + '}\n'); }else{ object[methodName] = newMethod; } } }
Hmm... It apparently didn't notice my four-space indentation...
function chain(object, methodName, newMethod){ if(object && typeof object == 'object' && methodName && typeof methodName == 'string' && newMethod && typeof newMethod == 'function'){ var old = object[methodName]; if(old && typeof old == 'function'){ var oldArgs = []; var newArgs = []; for(var i0 = 0; i0 < old.length; i0++){ oldArgs[i] = 'arg' + i0; } for(var i0 = 0; i0 < newMethod.length; i0++){ newArgs[i] = 'arg' + i0; } oldArgs = oldArgs.join(', '); newArgs = newArgs.join(', '); var args = old.length > newMethod.length ? oldArgs : newArgs; object[methodName] = eval('function(' + args + '){\n' + ' old.call(' + oldArgs + ');\n' + ' newMethod.call(' + newArgs + ');\n' + '}\n'); }else{ object[methodName] = newMethod; } } }Correction: change both instances of "[i]" to "[i0]" (I-zero).