Optimalworks Ltd web design, SEO, training and consultancy 

the site completely surpassed my expectations GT, AutoDirector, we build websites that work, my website is great - I love it MP, XL Autos, reach more customers, make more sales, it's more than I ever could have expected LM, Trade Cars UK, we can help your business grow, 99% of business now comes from my website GV, Bash Events, promoting standards throughout the web industry, your contribution added a level of quality we would otherwise have struggled to achieve PS, Hemsley Fraser
  1. home
  2. Services
  3. Portfolio
  4. Contact us
  5. blog

Cross-browser JavaScript - detecting arrays

written by Craig, 11 October 2007

Checking whether a JavaScript variable is an array is incredibly awkward. Various solutions have been proposed, but just when you think you’ve got the ultimate solution, a particular browser pops up and bites you (yes, Safari, I’m talking about you!)

So why is this particular problem so difficult?

  1. Arrays are reported as being ‘objects’ - but so are lots of other things.
  2. Arrays have a length, but so do some DOM nodes (like form elements). Also, simple checks like ‘x.length’ will return false if the length doesn’t exist or if it’s zero.
  3. Arrays have constructors, but again, so do other things. If you use ‘toString’ on the constructor, most browsers report a function name of ‘Array’ … unfortunately, Safari chooses to return ‘function’.
  4. Some DOM collections are very similar to arrays, but aren’t!

After much swearing, I’ve finally come up with a solution: function IsArray(array) { return !( !array || (!array.length || array.length == 0) || typeof array !== ‘object’ || !array.constructor || array.nodeType || array.item ); } // examples alert( IsArray( [] ) ); // true alert( IsArray( [1,2,3] ) ); // true alert( IsArray( {a:1} ) ); // false alert( IsArray( document.forms[0] ) ); // false alert( IsArray( [document.forms[0]] ) ); // true

It appears to work consistently in IE5.0+, Firefox, Safari, Opera and Konqueror, but I still have a nagging doubt that I’ve missed something?

Does anyone have anything better?

  • categories: javascript
  • tags: firefox, ie, javascript, safari
  • trackback: http://www.optimalworks.net/blog/2007/web-development/javascript/array-detection/trackback
  • bookmark: del.icio.us, digg, facebook, reddit, Furl, Spurl, Blinklist, Slashdot, Technorati, Yahoo!

5 comments:

  1. 11 October 2007 steve commented

    your code on the last test is missing a closing square bracket. ;-)

  2. 11 October 2007 steve commented

    Test 1
    fails in Safari 3.0 (at least in windows)

  3. 11 October 2007 Craig commented

    Opps - I’ve fixed the missing ‘]’.Sorry for any confusion!
    Was that the cause of the Safari problem? It seems OK to me?

  4. 11 October 2007 los commented

    What’s wrong with the simplest way (obj && obj.constructor == Array)?

  5. 12 October 2007 Craig commented

    That’s bizarre - obj.constructor == Array does return ‘true’ correctly in Safari?

    However, if you do examine obj.constructor or obj.constructor.toString(), it returns just “function”. That causes most solutions I’ve found to fail.

    I want to do some further testing, but this does appear to work in all browsers…

    function IsArray(array) { return !!(array && array.constructor == Array); }

    Thanks for that!

add your comments
please enter your name
please enter your email address - it will not be published anywhere
please enter your website address
please enter your comments (all are moderated and checked for spam)
  • next article: Search engine sitemap auto-discovery 
  • previous article: TACS 1.11 update - free PHP template system 
search
  • all popular (5)
  • courses (1)
  • general (3)
  • hardware (1)
  • software (24)
    • free download (6)
    • web browsers (9)
    • web servers (2)
  • web development (31)
    • accessibility (4)
    • ajax (1)
    • css (2)
    • graphic design (4)
    • html (1)
    • javascript (7)
    • php (4)
    • seo (1)

tags

  • accessibility
  • ajax
  • apache
  • award
  • computing
  • css
  • design
  • developers
  • dom
  • download
  • editor
  • firefox
  • funny
  • graphics
  • hardware
  • html
  • ie
  • javascript
  • opera
  • php
  • portfolio
  • review
  • safari
  • seo
  • server
  • software
  • ssl
  • standards
  • wordpress
  • xml
  1. site map
  2. home
  3. Services
  4. Portfolio
  5. Contact us
  6. blog
  7. RSS feed
  8. project survey

XHTML 1.0 | CSS 2.1 | WAI AAA | printer-friendly

©2008 Optimalworks Ltd, Devon, UK. Registered in England and Wales No. 5922205.

This page can be viewed at http://www.optimalworks.net/blog/2007/web-development/javascript/array-detection