www.webdeveloper.com
Results 1 to 7 of 7

Thread: indexOf in < IE9

  1. #1
    Join Date
    May 2003
    Location
    UK
    Posts
    220

    indexOf in < IE9

    IE8 reports indexOf is not a supported method. I'm using this to get the value of an array item.

    However, MS say IE6+ should be supported; http://msdn.microsoft.com/en-us/libr...(v=vs.94).aspx

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    All String objects/literals have a indexOf method, if you however passed in a number or boolean etc, then you will have to convert it to String() first:

    Code:
    alert(  'Test'.indexOf('e')  ); //1
    alert(  String(123).indexOf('3')  ); //2
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    I am passing a string already;

    j = b.indexOf(photoUrl);

    with photoUrl being a parameter, such as /images/subfolder1/subfolder2/image.jpg

    Maybe the / in the string are ****ing it up?

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    776
    But what is the value of "b", as that is where the problem would b ?
    JavaScript: Learn | Validate | Compact

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by ldoodle View Post
    IE8 reports indexOf is not a supported method. I'm using this to get the value of an array item.

    However, MS say IE6+ should be supported; http://msdn.microsoft.com/en-us/library/53xtt423(v=vs.94).aspx
    That article refers to String().indexOf not Array().indexOf which is of much more recent introduction.

    Here's the crutchware that you need:
    Code:
    if( Array().indexOf === undefined )
    {
      Array.prototype.indexOf = function( item )
      {
        for( var i = this.length - 1; i > -1 && this[ i ] !== item; i-- )
        ;
      
        return i;
      }
    }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  6. #6
    Join Date
    May 2003
    Location
    UK
    Posts
    220
    Quote Originally Posted by Logic Ali View Post
    That article refers to String().indexOf not Array().indexOf which is of much more recent introduction.
    Thankyou very much. I have seen other variants of your code but they didn't seem to work.



    EDIT: I noticed yours uses triple = rather than double. Why? Also some of the other articles on the MS website use triple (http://msdn.microsoft.com/en-us/libr...155295(v=vs.94))
    Last edited by ldoodle; 08-19-2012 at 02:17 PM.

  7. #7
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by ldoodle View Post
    I noticed yours uses triple = rather than double. Why? Also some of the other articles on the MS website use triple (http://msdn.microsoft.com/en-us/library/jj155295(v=vs.94))
    Strict type checking is always good practice.

    Actually I now realise that code isn't right, and having looked at the specs, the function allows an optional starting index to be used, so I re-wrote it:
    Code:
    Array.prototype.indexOf = Array.prototype.indexOf || function( item, idx )
    {
      var retVal = 0,
          startIndex = ( typeof idx === 'number' ? Math.abs( Math.floor( idx ) ) : 0 );
           
      for( var i = startIndex, len = this.length; i < len && this[ i ] !== item; i++ )
      ;
      
      return i == len ? -1 : i;
    }
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles