Javascript Explode Issue
Solution 1:
This isn't suited for a comment, so I'll try to write what I think people are saying with respect to using JSON instead of your strings:
This:
test~63~Dave Sanders~http://graph.facebook.com/998433599/picture?type=large~Dave Sanders~9 minutes ago
Could become this:
[
{
'name': 'test',
'id': 63,
'name': 'Dave Sanders',
'url': 'http://graph.facebook.com/998433599/picture?type=large',
'when': '9 minutes ago'
// other properties
},
{
// the next one
},
{
// etc.
}
]
It may look like more work, but you could use some JSON library for php (I'm guessing, since you use the term "explode") to build the JSON on the server. Then your returned content is already in a form for your browser-based client app to use with no additional effort. I find it hard to imagine that this will be more work in the end than what you're trying to do with the string manipulation, and JSON is likely to be less brittle.
Solution 2:
As per your updated question...
You can still use JSON for this. I think it might help you to read the JSON specification
Adding to Chris Farmer's answer, JSON can be made of Arrays as well, so you can nest your comments in the response:
var response = [
{
'name': 'test',
'id': 63,
'name: 'Dave Sanders',
'url': 'http://graph.facebook.com/998433599/picture?type=large',
'comments' : [
'comment1',
'comment2',
'comment3',
...
]
},
...
]
You can then loop through the comments like this:
// loop through the images
for ( var i = 0; i < response.length; i++ ) {
var image = response[i];
// loop through the comments
for ( var z = 0; z < response.length; z++ ) {
var comment = image.comments[z];
// insert HTML here.
}
}
Do you see how much easier and less verbose this is than your current method?
Unfortunately to make this easier for yourself in the long run you will have to re-write your backend code. However, this will teach you the benefit and power of using JSON to transport information.
Post a Comment for "Javascript Explode Issue"