I ran across this error recently using a javascript library which changes the parameters of a function based on conditions.
You can test an object to see if it has the function. I would only do this in scenarios where you don't control what is getting passed to you.
if( param.indexOf != undefined ) {
// we have a string or other object that
// happens to have a function named indexOf
}
You can test this in your browser console:
> (3).indexOf == undefined;
true
> "".indexOf == undefined;
false