Tuesday, June 30, 2015

Fix for undefined Javascript indexOf() function in IE 8

I suggest you use JQuery to address cross-browser compatibility issues like the support for the indexOf() function of an Array.

Put the code below at the top of your javascript file, after $(document).ready(function() {....}).

//if browser does not support Array.indexOf() method (IE8 and IE9) 
//use jQuery.inArray() method instead 
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(val) {
        return jQuery.inArray(val, this);

This works perfect if you want to keep using the indexOf() function. This code provides a fallback for your site visitors who are using IE 8 and older browsers that does not support the Javascript indexOf() function for Array.

No comments:

Post a Comment