2013-05-29 19 views
6

Sto utilizzando un'implementazione javascript dell'algoritmo gzip che funziona perfettamente con Firefox e Chrome. Ma con Internet Explorer ho ottenuto il seguente errore:JavaScript: Metodo forOach non supportato da Internet Explorer

Method forEach is not supported!

Codice:

deflate.deflate(data, level).forEach(function (byte) { 
    putByte(byte, out); 
}); 

sto utilizzando Internet Explorer 9, che dovrebbe sostenere il metodo foreach.

Qualche idea?

Grazie mille!

+1

forEach non è supportato in IE8. IE9 dovrebbe supportarlo, tuttavia. http://kangax.github.io/es5-compat-table/#Array.prototype.forEach –

+2

La tua pagina è in esecuzione in quirks mode in IE? (Forse per caso?) '. ForEach()' è supportato solo in modalità standard. 'Deflate.deflate()' restituisce sempre un array? – nnnnnn

+0

Invece di estendere un oggetto built-in, è possibile sostituire la parte 'forEach' con un ciclo' for' a 2 righe. – RobG

risposta

17

Si potrebbe provare ed estendere l'oggetto Array per i browser che non supportano il metodo foreach su di essa come suggerito qui Array.forEach

Un esempio è:

if (!Array.prototype.forEach) { 
    Array.prototype.forEach = function(fn, scope) { 
     for(var i = 0, len = this.length; i < len; ++i) { 
      fn.call(scope, this[i], i, this); 
     } 
    } 
} 
+0

Questo ha funzionato bene! Grazie mille! –

0

forEach non è supportato in IE9, puoi provare a usare jquery.
es:

$. each (function (byte) { 
    putByte(byte, out); 
}); 
+2

Voglio evitare l'uso di jQuery. –

Problemi correlati