我在我的Facebook應用程序中使用了標準的Facebook Javascript代碼,這是我在Heroku的示例應用程序中獲得的。我不太瞭解它,但通過小心,我已經能夠修改示例應用程序來完成對我有用的一些基本事項。爲什麼Facebook Javascript SDK異步加載?
我的理解是,這段代碼以某種方式加載了Facebook Javascript SDK。也就是說,它會加載一個javascript代碼文件。它真的加載了什麼文件?爲什麼我不能像加載任何其他JavaScript代碼文件一樣加載它?例如:
<script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>
這種高度複雜的方法的缺點是,我不能讓它的正面或反面,因爲我不明白,我不知道如何使用它的直覺。
代碼在這裏:
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo AppInfo::appID(); ?>', // App ID
channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Listen to the auth.login which will be called when the user logs in
// using the Login button
FB.Event.subscribe('auth.login', function(response) {
// We want to reload the page now so PHP can read the cookie that the
// Javascript SDK sat. But we don't want to use
// window.location.reload() because if this is in a canvas there was a
// post made to this page and a reload will trigger a message to the
// user asking if they want to send data again.
window.location = window.location;
});
FB.Canvas.setAutoGrow();
};
// Load the SDK Asynchronously
(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/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
爲什麼有必要在Facebook Javascript SDK中使用這種技術,當它幾乎不用於任何其他Javascript代碼? – 2013-02-22 12:23:17
Facebook是腳本嵌入到數百萬個網頁中的大玩家之一,因此他們必須更多關注性能問題。環顧四周,我想你會發現其他大玩家通常也會推薦嵌入他們的腳本,f.e.谷歌,Twitter,... – CBroe 2013-02-22 13:32:01
而在Facebook的情況下,他們的服務器在某些時刻可能會變慢。只需加載一個按鈕時請注意。通常會有輕微的延遲。 – Tom 2013-02-22 15:57:11