views:

390

answers:

3

The following Javascript displays in Safari but not Mobile Safari. Can anyone see any bugs?

$("#results").append(data);

var songdata = JSON.parse(data);

var i = 0;

for (i=0;i<=songdata.total;i++)
{
    alert(i);
    var songhtml = "<ul><li><img src=\"" + songdata.data[i].artwork + "\" /></li><li>" + songdata.data[i].title + "</li><li>" + songdata.data[i].artist + "</li><li>" + songdata.data[i].length + "</li><li>" + songdata.data[i].listen + "</li></ul>";
    $("#results").append(songhtml);

}

Thanks in advance.

A: 

The for (i=0;i<=songdata.total;i++) looks suspicious to me. You're starting with zero, but continuing equal to or greater than total, so you're going to loop total + 1 times. (I take it this isn't a JavaScript/JSON array, as you're using total rather than length.)

If total tells you how many entries there are, and if the entries start at 0, then use just <. If the entries start at 1, do that. :-) But if you reference an invalid songdata[i], then songdata[i].artwork will fail because songdata[i] will be undefined.

T.J. Crowder
+3  A: 

JSON.parse is not official Javascript, its not supported in all browsers. That could be your problem, but I don't have mobile safari to test it on.

http://www.json.org/json2.js

Download this file and

<script type="text/javascript" charset="utf-8" src="/js/JSON2.js"></script>
MindStalker
Yes, I've done some testing and that's the case. What are the alternatives to this function?
Ben
Thank you very much. Working a treat!
Ben
+1  A: 

I have to second MindStalkers comment. Id bet its your use of the non cross browser JSON.parse. In addition to that your loop structure looks suspicious - plus it doesnt make much sense to me to why you are using a standard loop instead of jQuery.each().

prodigitalson