[英]How to get the facebook user id to another javascript file after login?
我了解可以通過以下方式獲取Facebook ID:
FB.api('/me', function(response) {
alert('Your id is ' + response.id);
});
但是,我想讓用戶登錄,然后在另一個文件中獲取該ID,以便我進行處理。 現在我有:
var id = "";
var fburl = "http://graph.facebook.com/" + id + "?callback=?"
$(function(){
$("#fb-profile-picture")[0].src = "http://graph.facebook.com/" + id +"/picture";
$.getJSON(fburl, function(data){
console.log(data);
$("#name").append(data.name);
$("#user-id").append(data.id);
});
});
並且,如果我在id
var中手動輸入id
,它可以工作,但是我希望能夠將那個response.id
作為值,並在其他JavaScript文件中使用它,但我還沒有弄清楚該怎么做。
假設你
盡管不認為最佳做法,但進行全局和觸發事件可能是解決方案。
腳本1:
FB.api('/me', function(response) {
window.fbCustomId = response.id;
console.log(window.fbCustomId);
$(window).trigger('fbResponseLoaded');
});
腳本2:
function renderProfile() {
var fburl = "//graph.facebook.com/" + window.fbCustomId + "?callback=?"
$(function () {
$("#fb-profile-picture")[0].src = "//graph.facebook.com/" + window.fbCustomId + "/picture";
$.getJSON(fburl, function (data) {
console.log(data, window.fbCustomId);
$("#name").append( $('<span>').text(data.name) );
$("#user-id").append( $('<span>').text(data.id) );
});
});
}
if (window.fbCustomId) {
renderProfile();
} else {
$(window).on('fbResponseLoaded', renderProfile);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.