July 30, 201412 yr Hi, I have this piece of JS which displays photos based on flickr tags. It all works fine but I somehow need to add an if statement wherein if there are no results for the API search a simple 'No photos found' paragraph is shown instead. I've no idea where I would add it in though, so if someone can give some pointers? The script: $(function() { var apiKey = 'REMOVED'; var tag = flickr_tag; var perPage = '25'; var showOnPage = '25'; var page = '1'; $.getJSON('https://api.flickr.com/services/rest/?format=json&method='+ 'flickr.photos.search&api_key=' + apiKey + '&tags=' + tag + '&per_page=' + perPage + '&jsoncallback=?', function(data){ var classShown = 'class="fancybox" data-fancybox-group="thumbs"'; var classHidden = 'class="fancybox hidden"'; $.each(data.photos.photo, function(i, rPhoto){ var basePhotoURL = 'http://farm' + rPhoto.farm + '.static.flickr.com/' + rPhoto.server + '/' + rPhoto.id + '_' + rPhoto.secret; var thumbPhotoURL = basePhotoURL + '_s.jpg'; var mediumPhotoURL = basePhotoURL + '.jpg'; var photoStringStart = '<a '; var photoStringEnd = 'title="' + rPhoto.title + '" href="'+ mediumPhotoURL +'"><img src="' + thumbPhotoURL + '" alt="' + rPhoto.title + '"/></a>;' var photoString = (i < showOnPage) ? photoStringStart + classShown + photoStringEnd : photoStringStart + classHidden + photoStringEnd; $(photoString).appendTo("#flickr"); }); $("a.fancybox").fancybox(); }); });
July 30, 201412 yr Try wrapping it around the function(data) section and checking if any data has been returned from the getJSON request.
Create an account or sign in to comment