Posted By

pixelatorz on 09/15/11


Tagged

javascript internet explorer foreach


Versions (?)

enable forEach in older internet explorers


 / Published in: JavaScript
 

found the solution here: http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc

  1. // Add ECMA262-5 method binding if not supported natively
  2. //
  3. if (!('bind' in Function.prototype)) {
  4. Function.prototype.bind= function(owner) {
  5. var that= this;
  6. if (arguments.length<=1) {
  7. return function() {
  8. return that.apply(owner, arguments);
  9. };
  10. } else {
  11. var args= Array.prototype.slice.call(arguments, 1);
  12. return function() {
  13. return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
  14. };
  15. }
  16. };
  17. }
  18.  
  19. // Add ECMA262-5 string trim if not supported natively
  20. //
  21. if (!('trim' in String.prototype)) {
  22. String.prototype.trim= function() {
  23. return this.replace(/^\s+/, '').replace(/\s+$/, '');
  24. };
  25. }
  26.  
  27. // Add ECMA262-5 Array methods if not supported natively
  28. //
  29. if (!('indexOf' in Array.prototype)) {
  30. Array.prototype.indexOf= function(find, i /*opt*/) {
  31. if (i===undefined) i= 0;
  32. if (i<0) i+= this.length;
  33. if (i<0) i= 0;
  34. for (var n= this.length; i<n; i++)
  35. if (i in this && this[i]===find)
  36. return i;
  37. return -1;
  38. };
  39. }
  40. if (!('lastIndexOf' in Array.prototype)) {
  41. Array.prototype.lastIndexOf= function(find, i /*opt*/) {
  42. if (i===undefined) i= this.length-1;
  43. if (i<0) i+= this.length;
  44. if (i>this.length-1) i= this.length-1;
  45. for (i++; i-->0;) /* i++ because from-argument is sadly inclusive */
  46. if (i in this && this[i]===find)
  47. return i;
  48. return -1;
  49. };
  50. }
  51. if (!('forEach' in Array.prototype)) {
  52. Array.prototype.forEach= function(action, that /*opt*/) {
  53. for (var i= 0, n= this.length; i<n; i++)
  54. if (i in this)
  55. action.call(that, this[i], i, this);
  56. };
  57. }
  58. if (!('map' in Array.prototype)) {
  59. Array.prototype.map= function(mapper, that /*opt*/) {
  60. var other= new Array(this.length);
  61. for (var i= 0, n= this.length; i<n; i++)
  62. if (i in this)
  63. other[i]= mapper.call(that, this[i], i, this);
  64. return other;
  65. };
  66. }
  67. if (!('filter' in Array.prototype)) {
  68. Array.prototype.filter= function(filter, that /*opt*/) {
  69. var other= [], v;
  70. for (var i=0, n= this.length; i<n; i++)
  71. if (i in this && filter.call(that, v= this[i], i, this))
  72. other.push(v);
  73. return other;
  74. };
  75. }
  76. if (!('every' in Array.prototype)) {
  77. Array.prototype.every= function(tester, that /*opt*/) {
  78. for (var i= 0, n= this.length; i<n; i++)
  79. if (i in this && !tester.call(that, this[i], i, this))
  80. return false;
  81. return true;
  82. };
  83. }
  84. if (!('some' in Array.prototype)) {
  85. Array.prototype.some= function(tester, that /*opt*/) {
  86. for (var i= 0, n= this.length; i<n; i++)
  87. if (i in this && tester.call(that, this[i], i, this))
  88. return true;
  89. return false;
  90. };
  91. }

Report this snippet  

You need to login to post a comment.