Javascript add events cross-browser function implementation: use attachEvent/addEventListener vs inline events

12,271

Solution 1

With the 2nd solution, you have to manually call the previous functions, making it hard to remove specific listeners (which, to me, sounds like something you'd rather want than clearing all listeners), while on the first solution, you can only clear them all at the same time, unless you want to emulate the first functionality.

Personally, I always use the first solution, because it has the advantage of not having to worry about clearing possible other event listeners.

The mozilla wiki also lists the advantages that the first solution works on any DOM element, not just HTML elements, and that it allows finer grained control over the phase when the listener gets activated (capturing vs. bubbling) with the third argument.

Solution 2

i would use both codes like this

function addEvent(html_element, event_name, event_function) {
    if (html_element.addEventListener) { // Modern
        html_element.addEventListener(event_name, event_function, false);
    } else if (html_element.attachEvent) { // Internet Explorer
        html_element.attachEvent("on" + event_name, event_function);
    } else { // others
        html_element["on" + event_name] = event_function;
    }
};
Share:
12,271
Marco Demaio
Author by

Marco Demaio

Just love coding all day long! Now I'm using PHP and Javascript for my daytime job. I took part in realizing all the back end application to handle the orders, contracts and invoices for a site that sells posta certificata per aziende. Language I love most is C++ Language I hate most is VB6/VB.NET Wishes: to see PHP becoming more OO with operator overloading, and Python add curly braces. I have started coding in BASIC since I was a 13 years old kid with a Commodore 64 and Apple II. During University they taught me C and C++ and JAVA and I realized even more how much I love to code. :) Funny stuff: Micro Roundcube plugin to improve the search box

Updated on July 20, 2022

Comments

  • Marco Demaio
    Marco Demaio almost 2 years

    In order to add events we could use this simple 1st solution:

    function AddEvent(html_element, event_name, event_function) 
    {       
       if(html_element.attachEvent) //Internet Explorer
          html_element.attachEvent("on" + event_name, function() {event_function.call(html_element);}); 
       else if(html_element.addEventListener) //Firefox & company
          html_element.addEventListener(event_name, event_function, false); //don't need the 'call' trick because in FF everything already works in the right way          
    } 
    

    or this 2nd solution (that adds inline events):

    function AddEvent(html_element, event_name, event_function) 
    {       
       var old_event = html_element['on' + event_name];
       if(typeof old_event !== 'function')
          html_element['on' + event_name] = function() { event_function.call(html_element); };
       else
          html_element['on' + event_name] = function() { old_event(); event_function.call(html_element); };
    }
    

    These are both cross-browsers and can be used in this way:

    AddEvent(document.getElementById('some_div_id'), 'click', function() 
    {             
       alert(this.tagName); //shows 'DIV'
    });  
    

    Since I have the feeling attachEvent/addEventListener are used more around in events handling implementations, I'm wondering:

    Are there any disadvantages/drawbacks against using the 2nd solution that I might better be aware of?

    I can see two, but I'm interested in more (if any):

    1. the 2nd solution screws up innerHTML of elements by adding events inline
    2. Using 2nd solution I can easily remove all functions associated with a certain event type (html_element['on' + event_name] = null), but I can not use detachEvent/removeEventListener to remove exactly a specific function.

    Any answers like: "use jQuery" or any other FW are pointless!