0

我正在做一个很棒的项目,通常我使用 AS3,但现在我使用的是 Javascript。

我有以下信息:下面我正在尝试访问检测线,基本上我需要的是检测(response.username)。我已经尝试了很多东西,有人可以帮忙吗?

<div id="fb-root"></div>
<script type="text/javascript">


$(document).ready(function(){


    var appId = "121070974711874";
    // If logging in as a Facebook canvas application use this URL.
    var redirectUrl = "http://apps.facebook.com/bggressive";
    // If logging in as a website do this. Be sure to add the host to your application's App Domain list. 
    var redirectUrl = window.location.href;




    // If the user did not grant the app authorization go ahead and
    // tell them that. Stop code execution.
    if (0 <= window.location.href.indexOf ("error_reason"))
    {
        $(document.body).append ("<p>Authorization denied!</p>");
        return;
    }


    // When the Facebook SDK script has finished loading init the
    // SDK and then get the login status of the user. The status is
    // reported in the handler.
    window.fbAsyncInit = function(){


        FB.init({
            appId : appId,
            status : true,
            cookie : true,
            oauth : true
        });


        FB.getLoginStatus (onCheckLoginStatus);
    };



    (function(d)
    {
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));



    function onCheckLoginStatus (response)
    {
        if (response.status != "connected")
        {
            top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (redirectUrl) + "&scope=user_photos,friends_photos";
        }
        else
        {$
            // Start the application (this is just demo code)!
            $(document.body).append ;

            FB.api('/me?fields=username', function(response) {
                function detect(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");








            });



            FB.api('/me/friends?fields=id,first_name', function(response) {
                var randomFriend = Math.floor(getRandom(0, response.data.length));
                gFriendID = response.data[randomFriend].id;
                var randomFriend = Math.floor(getRandom(0, response.data.length));
                gFriendID2 = response.data[randomFriend].id;



                function friend1(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                friend1("https://graph.facebook.com/" + gFriendID + "/picture?width=200&height=200");


                function friend2(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                friend2("https://graph.facebook.com/" + gFriendID2 + "/picture?width=200&height=200");


            });





        }
    }
});
</script>
4

1 回答 1

0

如果我理解正确,您想显示当前登录用户的图像,然后detect在图像加载时显示。您正在尝试修复这部分代码:

FB.api('/me?fields=username', function(response) {
    function detect(URL) {
        var image = new Image();
        image.src = URL;
        image.onload = function() {
            var result = 'result'; // An example result

        }

        document.body.appendChild(image)

    }
    detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");
});

这里发生了很多事情。首先,您需要知道要加载哪个图像的所有信息都将从/me/调用中提供给您。您唯一需要传递给函数的是回调(图像加载时要执行的操作),可能还有宽度和高度。

其次,如果您设置src图像的属性,图像将被加载。如果onload在属性之后设置处理程序src,并且从缓存中获取图像,则图像已经完成加载,onload事件已经触发,并且不会调用处理程序。这在这篇文章中得到了更好的解释。

所以,你可以这样做:

function detect(username) {
    var result = 'result'; // An example result
}

function displayPicture(width, height, callback) {
    FB.api('/me?fields=username', function(response) { 
        var image = new Image();
        image.onload = function() {
            callback(response.username);
        };
        image.src = "https://graph.facebook.com/" + response.username + "/picture?width=200&height=200";
        document.body.appendChild(image);
    });
}

displayPicture(200, 200, detect);
// or
displayPicture(200, 200, function(username) {
    var result = 'result'; // An example result
));

好吧,祝你好运!我希望这实际上是你想要做的。

于 2013-07-25T06:55:51.903 回答