0

我試圖張貼到Facebook新聞源和彈出框不時出現。 我認爲這與回傳或什麼有關。 這是什麼發生: 的用戶輪空的東西,並得到發送到佩森(佩森是貝寶的瑞典語版本),在彈出窗口中,然後查懋聲在彈出的窗口向用戶發送回我的網頁仍。 這是一個運行代碼:Facebook張貼到新聞源不顯示FB沒有定義

function postToFeed() { 

try { 
    FB.getLoginStatus(function (response) { 
     if (response.status === 'connected') { 

      var obj = { 
       method: 'feed', 
       link: 'link', 
       picture: 'imagelink', 
       name: 'title', 
       caption: 'some text...', 
       description: 'some text...', 
       redirect_uri: 'url' 
      }; 

      function callback(response) { 
       document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; 
      } 

      FB.ui(obj, callback); 
     } 
     else { 
      alert("not connected") 
     } 
    }); 

} catch (e) { 
    alert(e.Message) 
} 
} 

現在什麼發生的是,我從我的嘗試捕捉得到的錯誤,它只是在說FB沒有定義

如果我用一個按鈕後運行此功能它工作得很好。 如果我僞造從payson的回發它也在工作,但不是如果我在新窗口中執行它。

我試圖把FB.init功能在我的功能,但沒有幫助。

感謝您的幫助!

新代碼

這是一個.js文件

var isLoaded = false; 
window.fbAsyncInit = function() { 
FB.init({ 
    appId: '00000000', // App ID 
    channelUrl: 'url', // Channel File 
    status: true, // check login status 
    cookie: true, // enable cookies to allow the server to access the session 
    xfbml: true, // parse XFBML 
    oauth: true // enable OAuth 
}); 

isLoaded = true; 
}; 

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

    (function (d, s, id) { 
var js, fjs = d.getElementsByTagName(s)[0]; 
if (d.getElementById(id)) return; 
js = d.createElement(s); js.id = id; 
js.src = "//connect.facebook.net/sv_SE/all.js#xfbml=1&appId=503537396325197"; 
fjs.parentNode.insertBefore(js, fjs); 
} (document, 'script', 'facebook-jssdk')); 


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


    function postToFeed() { 

    var obj = { 
     display: 'iframe', 
     method: 'feed', 
     link: 'url', 
     picture: 'url', 
     name: 'text', 
     caption: 'text', 
     description: 'text', 
     redirect_uri: 'url' 
    }; 

    function callback(response) { 
     document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; 
    } 

    FB.ui(obj, callback); 
    } 


    function init() { 
    FB.init({ 
    appId: '00000', 
    status: true, // check login status 
    cookie: true, // enable cookies to allow the server to access the session 
    xfbml: true, // parse XFBML 
    channelUrl: 'url', // custom channel 
    oauth: true // enable OAuth 
    }); 

    FB.getLoginStatus(function (response) { 
    if (response.status == 'connected') { 
     var user_id = response.authResponse.userID; 
     var access_token = response.authResponse.accessToken 
     var page_id = "00000"; //page id 
     var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid=" + user_id; 
     var the_query = FB.Data.query(fql_query); 

     the_query.wait(function (rows) { 

      if (rows.length == 1 && rows[0].uid == user_id) { 
       $("#container").show(); 
       $("#container_notlike").hide(); 
       $("#notloggedin").hide(); 

      } 

      else { 
       $("#container_notlike").show(); 
       $("#container").hide(); 
       $("#notloggedin").hide(); 

       login(); 
      } 
     }); 
    } else { 
     $("#container_notlike").hide(); 
     $("#container").hide(); 

     login(); 

    } 
    }); 

    } 

     init(); 

     // whenever the user logs in, we refresh the page 
    FB.Event.subscribe('auth.login', function (response) { 
    top.location.href = "url"; 
    }); 


    FB.Event.subscribe('auth.logout', function (response) { 
     top.location.href = "url"; 
    }); 




     function login() { 
    FB.login(function (response) { 
     if (response.authResponse) { 
     console.log('Logged in'); 
     } else { 
     console.log('User cancelled login or did not fully authorize.'); 
     } 
     }, { scope: 'email,user_likes,publish_actions' }); 
     } 



    function likereload() { 

    FB.Event.subscribe('edge.create', function (response) { 

     top.location.href = "url" 
     }); 

    } 


    function getuserinfo() { 

     FB.getLoginStatus(function (response) { 
      if (response.authResponse) { 

      FB.api('/me', function (response) { 
      document.getElementById("txtfName").value = response.first_name; 
      document.getElementById("txtlName").value = response.last_name; 
      document.getElementById("txtemail").value = response.email; 

      }); 

     } 
     }); 


     } 
+0

嗯......你叫FB.init 3(!)倍在這個代碼中......只有第一個應該是需要的。 – luschn

回答

0

我解決了這個問題,

window.setTimeout('postToFeed()', 3000); 

然後,所有Facebook的代碼有時間來加載造就了我之前的飼料對話框

0

「沒有定義FB」 的意思是你沒有加載/初始化的JavaScript SDK。

https://developers.facebook.com/docs/reference/javascript/

見「加載和初始化」的例子,如何使用JavaScript SDK,鴕鳥政策在你的自定義函數的是放東西的。

此外,您還需要鴕鳥政策「getLoginStatus」,飼料對話也沒有工作,用戶登錄,這樣調用「getLoginStatus」是毫無意義的,只是需要時間。我也會刪除try/catch塊,你不需要它。

+0

有時fb沒有定義錯誤顯示出來,但共享框希望在同一時間出現像魔術一樣。 我知道我不應該需要這些東西,我只是絕望。 我試圖做的jQuery文件準備,但沒有工作乙醚 –

+0

然後它被調用的地方。請看看我發佈的鏈接,你需要添加的所有內容都在那裏。 – luschn

+0

是的,謝謝你我已經閱讀過這個頁面,當我用按鈕彈出分享框時,它工作的很好,但是我希望它在頁面自動加載時彈出。這是理所當然的。 –