在mobileinit事件上加载Facebook API:jQuery mobile

时间:2013-07-28 16:40:28

标签: jquery facebook jquery-mobile facebook-javascript-sdk

问题是在Facebook API准备好之前加载了页面。如果用户单击Facebook连接按钮,则不会定义变量FB。

目前,我在document.ready上加载了Facebook API。

我找到了触发自定义事件的解决方法。

在mobileinit事件中加载Facebook API是一个很好的解决方案吗?它将在所有其他脚本之前加载并且只加载一次吗?

代码:

$(document).on("mobileinit", function(){
  $.ajaxSetup({ cache: true });
    $.getScript('//connect.facebook.net/en_US/all.js', function(){
        window.fbAsyncInit = function() {
            FB.init({
                appId: 'appid',
                channelUrl: 'channel.html',
                //status: true,
                cookie: true,
                xfbml: true
            });
            $(document).trigger("facebook:ready");
        }
    });
});

感谢您的回答

1 个答案:

答案 0 :(得分:0)

使用jQuery-Mobile(JQM)构建的应用程序是single-page-application

“$(document).ready”只会在加载第一个JQM页面之前触发一次。您可以在“$(document).ready”中加载FB库。

来自this post,更多的是抱怨“$(document).ready”只被解雇一次,在你的情况下,你可以利用它。