[英]Javascript: Run function when page has finished loading, script is loading async
[英]Running a javascript function when Facebook has finished loading
我正在编写一个Facebook应用程序,该应用程序使用badgeville向玩家奖励积分。 有时页面加载时需要奖励积分,但是我在页面上也有一些Facebook社交插件,例如评论框和按钮。 看来,由于页面正在等待这些内容加载,因此它不会及时加载badgeville内容,从而使授予积分的功能无法正常运行。
Facebook完成操作后,是否可以使用任何事件来运行该功能? 我发现“ FB.Canvas.setDoneLoading();” 但不确定如何实施。
谢谢
假设您正在执行服务器端登录,则应使用FB.Event.subscribe 。 如果要初始化状态为true的FB对象,则在初始化FB对象后将触发auth.statusChange或auth.authResponseChange事件。
FB.Event.subscribe('auth.statusChange', function(response) {
if(response.status == 'connected') {
runFbInitCriticalCode();
}
});
FB.init({
appId : 'appId',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
当然,您可能会有这样的事情:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelUrl : 'http://www.yourdomain.com/channel.html', // Custom Channel URL
oauth : true //enables OAuth 2.0
});
FB.Canvas.setDoneLoading(
function () {
alert("Done loading");
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
这将确保在初始化FB函数之前不会调用setDoneLoading()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.