无法使Object退出功能

时间:2013-07-25 04:51:52

标签: javascript facebook function facebook-graph-api var

我正在开发一个很棒的项目,通常我使用的是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>

1 个答案:

答案 0 :(得分: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事件已经被触发,并且处理程序不会被调用。在this post中解释得更好。

所以,你可以这样做:

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
));
好吧,祝你好运!事实上,我希望这是你想要做的事情。