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:
-
function indexOf( $pArray, $pVal )
-
{
-
var $theIndex = -1;
-
for( var $i = 0; $i <$pArray.length; $i++ )
-
{
-
if ( $pArray[ $i ] == $pVal )
-
{
-
$theIndex = $i;
-
break;
-
}
-
}
-
return $theIndex;
-
}
Comments
4 Responses to “No .indexOf in WebKit?”
Leave a Reply
F-ing Safari…Oops, I’m a Mac-head now. I heart Safari.
It’s ok, all love contains an element of pain.
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
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.