I am a little confused with checking if an array value us empty from a json response.
{
"status": "success",
"message": "All pages re-ordered",
"content": {
"_wysihtml5_mode": "1",
"Page": {
"title": "cover",
"page_text": "dfvdfvdfvdfvdv story go daddy xxx",
"storyborad_img": "1jhkjh.png",
"background_url": "kjbj.png",
"newBackground_url": "",
"text_font": "arial",
"id": "30",
"book_id": "38",
"newStoryborad_img": {
"name": "1jhkjh.png",
"type": "image\/png",
"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpvyf8Xx",
"error": 0,
"size": 185607
}
},
"User": {
"username": "testuser"
}
}
}
I have tried to check typeof array == undefined and .length but both give me what is in the else statement newBackground_url
var page = $.parseJSON(xhr.responseText.replace('</p>', ''));
var imageType;
if(page.content.Page.newStoryborad_img.length > 0) {
imageType = page.content.Page.newStoryborad_img.name;
}
else {
imageType = page.content.Page.newBackground_url.name;
}
</p>in your JSON? The path should bepage.content.Page.storyborad_img[sic] according to your code.newStoryborad_imgan object? Or, do you receive anarraywhen there are more than one images?typeof(array) == undefinedshould give you the else statement, because you have defined it in your json. You might wanttypeof(array) != 'undefined'if you want its presence to trigger theifblock. (which is what it looks like you want it to do)