El siguiente código se puede ejecutar sin problemas en Chrome, pero arroja el siguiente error en Internet Explorer 11.
El objeto no admite la propiedad o el método 'startsWith'
Estoy almacenando la identificación del elemento en una variable. ¿Cual es el problema?
function changeClass(elId) { var array = document.getElementsByTagName('td'); for (var a = 0; a < array.length; a++) { var str = array[a].id; if (str.startsWith('REP')) { if (str == elId) { array[a].style.backgroundColor = "Blue"; array[a].style.color = "white"; } else { array[a].style.backgroundColor = ""; array[a].style.color = ""; } } else if (str.startsWith('D')) { if (str == elId) { array[a].style.backgroundColor = "Blue"; array[a].style.color = "white"; } else { array[a].style.backgroundColor = ""; array[a].style.color = ""; } } } } <table> <tr> <td id="REP1" onclick="changeClass('REP1');">REPS</td> <td id="td1"> </td> </tr> <tr> <td id="td1"> </td> <td id="D1" onclick="changeClass('D1');">Doors</td> </tr> <tr> <td id="td1"> </td> <td id="D12" onclick="changeClass('D12');">Doors</td> </tr> </table>String.prototype.startsWith es un método estándar en la versión más reciente de JavaScript, ES6.
Mirando la tabla de compatibilidad a continuación, podemos ver que es compatible con todas las principales plataformas actuales, excepto las versiones de Internet Explorer.
╔═══════════════╦════════╦═════════╦═══════╦═══════════════════╦═══════╦════════╗ ║ Feature ║ Chrome ║ Firefox ║ Edge ║ Internet Explorer ║ Opera ║ Safari ║ ╠═══════════════╬════════╬═════════╬═══════╬═══════════════════╬═══════╬════════╣ ║ Basic Support ║ 41+ ║ 17+ ║ (Yes) ║ No Support ║ 28 ║ 9 ║ ╚═══════════════╩════════╩═════════╩═══════╩═══════════════════╩═══════╩════════╝ Deberá implementar .startsWith usted mismo. Aquí está el polifill :
if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { position = position || 0; return this.indexOf(searchString, position) === position; }; }text.indexOf("newString") es el mejor método en lugar de startsWith .
Ejemplo:
var text = "Format"; if(text.indexOf("Format") == 0) { alert(text + " = Format"); } else { alert(text + " != Format"); }Si esto sucede en la aplicación Angular 2+, puede descomentar los polyfills de cadena en polyfills.ts :
import 'core-js/es6/string';