Warning: Cannot modify header information - headers already sent by (output started at /home/cssquirrel/www.cssquirrel.com/blog/index.php:4) in /home/cssquirrel/www.cssquirrel.com/blog/wp-includes/feed-rss2.php on line 8
CSSquirrel » json http://cssquirrel.com/blog opinions and news on web design Wed, 18 Jan 2012 18:10:41 +0000 en hourly 1 http://wordpress.org/?v=3.3.1 jQuery, JSON and IE – Getting Incorrect Array Length http://cssquirrel.com/blog/2008/10/14/jquery-json-and-ie-getting-incorrect-array-length/ http://cssquirrel.com/blog/2008/10/14/jquery-json-and-ie-getting-incorrect-array-length/#comments Tue, 14 Oct 2008 20:24:58 +0000 Kyle Weems http://www.cssquirrel.com/?p=106 Has anyone had the following problem? When loading a JSON array (through something like $.getJSON), Internet Explorer will sometimes report the length of the array being one higher than other browsers, with the extra element in the array being blank.

I don’t know if this is a problem in other libraries, but I suspect it is. But I do know that it’s happened to me when using jQuery’s Ajax functions.

Why is this occurring?

I was tormenting myself with this same question on a client project, and discovered that although a bit of browser quirkiness was involved, it was actually an error on my part.

I hate it when it’s me. ;)

As it turns out, I had an extra trailing comma at the end of the last element in my array. For example, in the following array:

{“gamesImDroolingFor”:[{"developer":"Blizzard","title:"Diablo III"},{"developer":"Square Enix","title":"Final Fantasy XIII"},]}

You’ll notice that there’s a comma after the last game’s bracket (which has been turned red for emphasis).

Firefox will ignore that comma, since clearly there’s no new object after it. Internet Explorer, the special child of the web, isn’t quite so bright, so it adds an extra, blank element to the array it creates.

Go IE.

The solution is easy, of course. Remove the unneeded comma.

I hope this helps prevent other developers from driving themselves batty trying to figure out what’s going wrong with their own arrays.

]]>
http://cssquirrel.com/blog/2008/10/14/jquery-json-and-ie-getting-incorrect-array-length/feed/ 6