简体   繁体   中英

Use JSON variable as public variable?

I'm loading my Flickr set into my website like so:

$(window).load(function() {
var apiKey = 'xxx';
var userId = '8378546@N08';

$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=1000' + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function(data) {
    $.each(data.photoset.photo, function(i, flickrPhoto){
        var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_z.jpg";  

        var fullPhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_l.jpg";          

        /*var FlickrLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";*/
        $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))

    });

    **$("<a href='#' class='zoom'/>").appendTo(".item");**
    **$("<a href='#' class='flickr'/>").appendTo(".item");**

    $("#photographs").gridalicious({
        gutter: 2,
        animate: true,
        effect: 'fadeInOnAppear',
        width: 320,
        complete: onComplete()
    });

    function onComplete(){
        $("#loader").delay(20000).fadeOut("slow");
        $(".item").hover(function(){
            $(".zoom, .flickr", this).stop(true,true).fadeIn(300);
        }, function () {
            $(".zoom, .flickr", this).stop(true,true).fadeOut(300);
        });
    }
});
});

The .zoom & .flickr divs are appended to the .item div (which contains the picture). I need both var fullPhotoURL and var FlickrLink since i want to use them in the highlighted (with stars) anchors in the code. When i do this, i get an error that both var fullPhotoURL and var FlickrLink cannot be found.

How should i solve this?

You have to define your variables outside the $.each block:

$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=1000' + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function(data) {

    var fullPhotoURL, FlickrLink;

    $.each(data.photoset.photo, function(i, flickrPhoto){
        var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_z.jpg";  

        fullPhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_l.jpg";          

        /*FlickrLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";*/
    $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))

   });

   $("<a href='#' class='zoom'/>").appendTo(".item");

To define a global variable in Javascript, you need to skip the var declaration term.

var "means" lacal variable

Start by assign a default value to your variables :

fullPhotoURL = null;
FlickrLink = null; //Avoid first upcase char to define variable

Then these variables will be available in all your functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM