Why I have this error: Object doesn't support

2020-04-02 06:45发布

问题:

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/.