We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

tylerhall on 02/28/07


Tagged

javascript event onload chain


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

noah
remysharp
vali29
SpinZ


Chain Javascript onload Events


Published in: JavaScript 


This lets you attach a new onload event without overwriting any previous events.

  1. function addLoadEvent(func) {
  2. var oldonload = window.onload;
  3. if(typeof window.onload != "function")
  4. window.onload = func;
  5. else
  6. window.onload = function() {
  7. oldonload();
  8. func();
  9. }
  10. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: Jesdisciple on November 10, 2007

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; } } }

Posted By: Jesdisciple on November 10, 2007

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;
        }
    }
}
Posted By: Jesdisciple on November 10, 2007

Correction: change both instances of "[i]" to "[i0]" (I-zero).

You need to login to post a comment.