2013-07-25 67 views
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> 

回答

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屬性,圖像將被加載。如果在src屬性後面設置onload處理程序,並且從緩存中獲取圖像,則圖像將已完成加載,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 
)); 

好吧,祝你好運!實際上,我希望這是你想要做的。

+0

嗨,沒有那是代碼的一部分,這是工作正常,我現在需要有用戶名基本上相同的驢行「detect()」,而不是 detect(「https:// graph.facebook.com/「+ response.username +」/ picture?width = 200&height = 200「); 我需要 detect(response.username); 我可以在這個代碼之外加載一個新函數或VAR –

+0

好吧,對。在你的代碼中,'detect()'函數將一個url加載到一個圖像中。你想要這個其他檢測功能做什麼?我想它不應該將'response.username'加載到圖像對象的src'字段中? –

+0

我只需要加載response.username,這樣我就可以使用它傳遞到此代碼之外的另一個函數 –