クロスブラウザのaddEventLisitener 2

by ethertank

JavaScript

var addListener = (function() {
    if (window.addEventListener) {
        return function(el, type, fn) {
            el.addEventListener(type, fn, false);
        };
    } else if (window.attachEvent) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.attachEvent('on' + type, f);
        };
    } else {
        return function(el, type, fn) {
            element['on' + type] = fn;
        };
    }
})();

//test
function myFunction(str) { document.write(str); }
myFunction("はろー");
addListener(document, "load", myFunction("はろはろー"));