如何在页面加载时触发FB.login()?

7

FB.login() 只有在点击事件触发时才有效。我希望它能在页面加载时自动触发。我尝试使用 trigger() 方法,但没有成功。我也尝试使用 jQuery('#button').click() 来触发它。

$(function(){
    //this is logging so I know the js is loading properly

    console.log("script loaded");

    $('button').button();

    $('#button').click(function(){   
        FB.login(function(response) {
            console.log('FBLOGIN firing');
            if(response.status === "connected"){
                var uID = response.authResponse.userID;
                FB.api('/me', function(response) {
                    var name = response.name;             
                    if(response.location){
                        var response = response.location.name;
                    }else{
                        alert("The Bringer Network needs your current city to be set in your Facebook 'about' section. Please make it public for our use");
                    } 
                });//closes fb.api
            }else if(response.status === "not_authorized"){
                //authCancelled. redirect
            }
        },{scope: 'user_location,user_likes'});
    });//closes click
    jQuery('#button').click();
});
2个回答

7

所以我放弃了将登录代码放在单独的.js文件中的想法。我把FB.login方法直接放在html中,与window.fbAsyncInit块以及FB.init代码一起。它完美地工作。

   <body>
        <div id="fb-root"></div>
    <script>

  window.fbAsyncInit = function() {

      FB.init({
        appId      : '140*****07', // App ID
        channelUrl : '//http://s******.com/channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        oauth      : true,
        xfbml      : true  // parse XFBML
      });

    FB.login(function (response) {

            if (response.status === "connected") {

                        var uID = response.authResponse.userID;

                        console.log(uID);

                      FB.api('/me', function (response) {

                             var name = response.name;
                             var locationName = ' ';

                             if (response.location) {

                                locationName = response.location.name;
                                console.log(locationName);

                            } else {

                                alert("your current city needs to be set in your Facebook 'about' section. Please make it public for our use");

                            }
                       });//closes fb.api

                    } else if (response.status === "not_authorized") {

                        //authCancelled. redirect
                    }
                },
                {
                    scope: 'user_location,user_likes'
                }
            );


      };//closes window.fbAsynInit

    // 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));

     </script> 

       <h1 id="redirecting">Redirecting</h1>
       <button id="button">click</button>

       <img id='backgroundImg' src="https://sa*****.com/template.jpg"/>

   </body>
</html>

3
将按钮点击处理程序函数移动到一个变量中:
var fbLogin = function () {
    // FB.login() ...
}

将其作为单击监听器:$('#button').click(fbLogin),并在页面加载时调用它。

完整的代码应该像这样:

var fbLogin = function () {
        FB.login(
            function (response) {
                if (response.status === "connected") {
                    var uID = response.authResponse.userID;

                    FB.api('/me', function (response) {
                        var name = response.name,
                            locationName;

                        if (response.location) {
                            locationName = response.location.name;
                        } else {
                            alert("The Bringer Network needs your current city to be set in your Facebook 'about' section. Please make it public for our use");
                        }
                    });//closes fb.api
                } else if (response.status === "not_authorized") {
                    //authCancelled. redirect
                }
            },
            {
                scope: 'user_location,user_likes'
            }
        );
    };

$(function () {
    $('#button')
        .button()
        .click(fbLogin);
});

window.fbAsyncInit = function () {
    // init the FB JS SDK
    FB.init({
        appId: 'YOUR_APP_ID', // App ID from the app dashboard
        status: true, // Check Facebook Login status
        xfbml: true // Look for social plugins on the page
    });

    // Call fbLogin
    fbLogin();
};

这仅在我点击按钮时才起作用。无论我先调用fbLogin()还是怎样,都不会在加载时起作用。 - Spilot
fbLogin正在触发,但FB.Login没有。 - Spilot
这是您用来嵌入 FB js 的代码吗?https://developers.facebook.com/docs/reference/javascript/ - bagonyi
如果是这样,请看一下编辑后的代码,现在应该没问题了。 - bagonyi
当我这样做时,整个脚本都会崩溃。为什么我需要将SDK添加到JS页面中?它已经在我的HTML中了。 - Spilot
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接