No .indexOf in WebKit?

Posted on December 11, 2006
Filed Under /dev/null/ | 61 views |

I assumed that .indexOf() was a standard function for Javascript arrays however it seems that Safari doesn't support it:

Value undefined (result of expression allowedChrArr.indexOf) is not object.
http://www.testsite.dev/arraytest.php

What the hell?

Rolling my own works instead but really, seems like that should be standard. Or am I just missing something?

PHP:
  1. function indexOf( $pArray, $pVal )
  2. {
  3.   var $theIndex = -1;
  4.   for( var $i = 0; $i <$pArray.length; $i++ )
  5.   { 
  6.     if ( $pArray[ $i ] == $pVal )
  7.     {
  8.       $theIndex = $i;
  9.       break;
  10.     }
  11.   }
  12.   return $theIndex;
  13. }

Comments

4 Responses to “No .indexOf in WebKit?”

  1. Mitchua on December 12th, 2006 12:55 am

    F-ing Safari…Oops, I’m a Mac-head now. I heart Safari.

  2. MrHappy on December 12th, 2006 1:11 am

    It’s ok, all love contains an element of pain.

  3. Miles on June 1st, 2007 10:10 am

    http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
    has a more elegant solution — just insert this:

    if (!Array.prototype.indexOf)
    {
    Array.prototype.indexOf = function(elt /*, from*/)
    {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from

  4. MrHappy on June 1st, 2007 1:53 pm

    That solution is indeed a complete solution and better than mine. But really, that ought to be in the core ECMA spec IMO. I find it pretty handy from time to time.

    Thanks for the link Miles.

Leave a Reply