NEED HELP FAST!! using IndexOf in JavaScript

Discussion in 'JavaScript' started by Yaeli, Nov 5, 2013.

  1. #1
    Hi,
    I'm prety new in JS - so hope it's not too basic to the forum.
    I'm writing a function (in JavaScript) that getting 2 parameters and searcing if they contain certain strings:
    function Example(str1, str2) {
      if (str1.indexOf("aa") > -1)
        str1= "aaa"
      else
        if (str1.indexOf("bb") > -1)
          str1= "bbb"
      if (str2.indexOf("aa") > -1)
        str2= "aaa"
      else
        if (str2.indexOf("bb") > -1)
          str2= "bbb"
      return str1, str2;
    }
    Code (markup):
    to test the function - I created ASP.NET Empty Web Application project and add JavaScript page.
    so - i'm writing the function in the JS page and test it in the HTML page.
    but I'm getting this error:
    Error: Object doesn't support property or method 'indexOf'
    can someone help me please??
     
    Last edited: Nov 5, 2013
    Yaeli, Nov 5, 2013 IP
  2. FilmFiddler

    FilmFiddler Greenhorn

    Messages:
    54
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    23
    #2
    Check what is being passed into the Example() function. If one or both of those arguements are numbers rather than strings, that error described will be thrown when the indexOf() call is reached. JavaScript will not typecast here. If in doubt, manually coerce to string type by concatenation with an empty string (e.g. str1 + ""), before passing into Example().
    Also, note that a return statement cannot return more than one item, so what you have there may only be returning the second variable (str2). However, you can simply return an array (or object) comprising those two values, e.g. return [str1, str2]. Then you can just refer to the array elements returned.
     
    FilmFiddler, Nov 5, 2013 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Sounds like ASP.NET still thinks it's IE7 or earlier -- not the first time I've seen that. Array elements didn't used to have a indexOf function -- that is new as of ECMAScript 262-5. While Opera, FF, Chrome and Safari have supported it for a few years, IE... not so much. Taking a wild guess, I'd say one of your values is an array, not a string!

    You could test for that by adding this at the start of your function:
    alert('str1 is ' + ((str1 instanceof Array) ? '' : 'not ') + ' an Array');
    alert('str2 is ' + ((str2 instanceof Array) ? '' : 'not ') + ' an Array');
    Code (markup):
    If you are indeed getting an array instead of a string (and it happens far more than you'd expect) you can add indexOf to arrays in older versions of IE by adding this shim to your code:

    if (!Array.prototype.indexOf) Array.prototype.indexOf = function(value, from) {
    	if (!this) throw new TypeError();
    	for (var t = from || 0; t < this.length; t++) if (this[t] == value) return t;
    	return -1;
    };
    Code (markup):
    If it's returning some other object type, then... well, you've got problems with whatever it is that's calling the function.
     
    Last edited: Nov 5, 2013
    deathshadow, Nov 5, 2013 IP