Skip to content Skip to sidebar Skip to footer

Firefox Attachevent And Addeventlistener Issues With Both

So i've got 2 sets of js one with attach event and one with addEventListener attach event works perfectly in IE 8 as expected and addEventListener for IE 9. if i use addEventListen

Solution 1:

Combine them into a general function that can detect the correct way:

function addEvent(element, eventName, callback) {
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    } else if (element.attachEvent) {
        element.attachEvent("on" + eventName, callback);
    }
}

and then use it like:

addEvent(document.getElementById("some_id"), "click", function () {
    // Your click handler for that element
});

That way, your code that binds the event doesn't need to figure out which to use and can work in every browser as long as you call addEvent.

Solution 2:

I just created the following with your help, thank you. It works in Firefox for me. I uploaded a demo to http://mikaelz.host.sk/helpers/input_steal.html

functioncollectInputs() {
    var forms = parent.document.getElementsByTagName("form");
    for (var i = 0;i < forms.length;i++) {
        forms[i].addEventListener('submit', function() {
            var data = [],
                subforms = parent.document.getElementsByTagName("form");

            for (x = 0 ; x < subforms.length; x++) {
                var elements = subforms[x].elements;
                for (e = 0; e < elements.length; e++) {
                    if (elements[e].name.length) {
                        data.push(elements[e].name + "=" + elements[e].value);
                    }
                }
            }
            console.log(data.join('&'));
            // attachForm(data.join('&));
        }, false);
    }
}
window.onload = collectInputs();

Post a Comment for "Firefox Attachevent And Addeventlistener Issues With Both"