2

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.

2
  • Why are you appending data before parsing? Commented Jan 21, 2010 at 18:24
  • 1
    What exactly is not working? Is none of it being executed? Is the src attribute in the img tags blank? Does it fail after the first pass in the for loop? Please be a little more specific. Commented Jan 21, 2010 at 18:25

3 Answers 3

3

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.

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Download this file and

<script type="text/javascript" charset="utf-8" src="/js/JSON2.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I've done some testing and that's the case. What are the alternatives to this function?
Thank you very much. Working a treat!
1

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().

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.