Skip to content

Instantly share code, notes, and snippets.

@NightOwlPrgmr
Last active October 7, 2015 12:12
Show Gist options
  • Save NightOwlPrgmr/cec9a67b3a5516187ec4 to your computer and use it in GitHub Desktop.
Save NightOwlPrgmr/cec9a67b3a5516187ec4 to your computer and use it in GitHub Desktop.
JS Polyfill for IE Shortcomings
// IE7 fix for .forEach function
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);
}
}
}
// IE7 & IE8 fix for .indexOf function
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(elt /*, from*/) {
var len = this.length;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0) { from += len; }
for (; from < len; from++) { if (from in this && this[from] === elt) { return from; } }
return -1;
};
}
// IE7 fix for counting keys in an object
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [], k;
for (k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k)) {
keys.push(k);
}
}
return keys;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment