Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help adding IF statement to flickr image displaying script

Featured Replies

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();
    });
});

 

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.