8

Getting an error using indexOf call in Javascript on Firefox and Opera. Works fine in IE.

Following is the error message:

Action

function anonymous(Grid, Row, Col, Event) { 
    return Grid.ActionShowPopupMenu(); 
} 

for event OnRightClick failed with exception: row.id.indexOf is not a function

I'm testing that a string contains another string in Javascript and using the indexOf function of a string. The calls however are being made in JQuery functions. Perhaps that is the reason for the problem? Is there an alternative to using indexOf in Javascript to test if a string contains another string? Is there a workaround for this problem?

Someth Victory
  • 4,492
  • 2
  • 23
  • 27
user840930
  • 5,214
  • 21
  • 65
  • 94
  • 2
    indexOf definitely works in FF. Been using it for not sure how long.. It might be a problem is with the object you're calling it with. – techfoobar Jun 12 '12 at 08:14
  • If you're passing jQuery objects, especially if `Row` is a jQ object, `.id` will be undefined/null. Either use `Row.getAttr('id').indexOf()` or use `Row.get(0).id.indexOf()`. If that fails, too: `Row` is capitalized in your function declaration, but the exception shows a lower case `row`. JS is CaseSensitive – Elias Van Ootegem Jun 12 '12 at 08:20

3 Answers3

6

String.indexOf is perfectly OK in all browsers. I assume the id property of your row object is no string (nor array, btw, because indexOf is also defined on arrays (except for IE))

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
3

indexOf is not okay for IE prior to IE9. If you want your code to work in ie < 9, you should define the method for non-compliant browsers in a common js file that can be dropped into every page. See this thread for more details. The code is taken from Mozilla

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 1) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}
Community
  • 1
  • 1
ronan_mac
  • 428
  • 7
  • 16
0

indexOf() is ok for all browsers. It is designed for both, String and Array, see this: http://jsfiddle.net/SquTp/

There is maybe something wrong with your dom selection, or you may use it in the wrong way.

Someth Victory
  • 4,492
  • 2
  • 23
  • 27