Why I have this error: Object doesn't support property or method 'forEach' for Internet Explorer?

别等时光非礼了梦想. 提交于 2019-12-01 14:10:35

问题


I am Working on Javascript on a jenkins plugin using maven and I have this code:

   function arrayElements(element, index, array) 
     {
         var arrayPaths = element.split("\\");
         var projectSource = arrayPaths[2];
         var array = element.split("_");
         if (projectSource === global ) {             
             if (array[2]===filtro){
             document.getElementById("source").options.add(new Option(arrayPaths[3], element));
             }
         }
     }
    function fillCompiledSource(object, projects)
    {
        document.getElementById("source").innerHTML = "";        
        global = document.getElementById("branches").value;     
        projects.forEach(arrayElements)
    }
    var projects = new Array();</script><script>
    function fillCombo()
    {
         document.getElementById("source").innerHTML = "";
         global = document.getElementById("branches").value;     
         var array = document.getElementById("branches").value.split('/');
         global = array[1];
         projects.forEach(arrayElements)       
    }

This fail only in internet explorer and only when document Mode is IE8 standarts I don't know what is the reason and how I can resolve that..

Pd: The Internet explorer is 10


回答1:


Yeap, its because IE8 does not implement Array.forEach (neither many other more modern JS methods). If you need to work in IE8, you will have to shim it (see the compatibility section).

By the way, MDN has resources for most of the other unsupported methods too.




回答2:


This might help. To solve the problem in jQuery:

//This will fail in IE8
myObject.each(function(index, value){
 //your code goes here
});

//This will work in IE8 and all modern browsers
$.each(myObject, function(index, value){
 //your code goes here
});



回答3:


You can also bind the unexisting forEach function to Array.prototype.forEeach.

(function () {
    if ( typeof NodeList.prototype.forEach === "function" ) return false;
    NodeList.prototype.forEach = Array.prototype.forEach;
})();

I found it in this post https://tips.tutorialhorizon.com/2017/01/06/object-doesnt-support-property-or-method-foreach/.



来源:https://stackoverflow.com/questions/20098953/why-i-have-this-error-object-doesnt-support-property-or-method-foreach-for-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!