JavaScript - Compatibility with Internet Explorer

user warning: Table 'diogomelo.ds_settings' doesn't exist query: ds_get_settings /* Anonymous : ds_get_settings */ SELECT module, type, build_mode, settings, fields FROM ds_settings WHERE module = 'nd' AND type = 'blog' AND build_mode = 'full' in /mnt/sdf/proj/diogomelo/sites/all/modules/ds/ds.module on line 980.

There is a few javascript functions that are very useful but are not implemented on IE: Function.bind, String.trim, Array.indexOf, Array.lastIndexOf and a few more...

I found an script on stackoverflow (lost the link) that tests it those functions exists and, if it doesn't, implement them. Also, as I use console.log for debugging, IE kept claiming console.log doesn't exists, so I added that too (dummy version, just to stop it from complaining).

Here is the script: ie-compatibility.js

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