简体   繁体   English

将Facebook fb:login-button转换为SDK javascript登录

[英]Transform Facebook fb:login-button into SDK javascript login

I need a custom facebook login button. 我需要一个自定义的Facebook登录按钮。 The fb:login-button solution with medium, large etc. is not what I need. fb:登录按钮解决方案有中,大等不是我需要的。 I know the FB TOS. 我知道FB TOS。 My button will be very similiar. 我的按钮非常相似。

How is it possible to tranform my button into a javascript onlick solution with a custom button? 如何使用自定义按钮将我的按钮转换为javascript onlick解决方案? The "onlogin="top.location.href='example.com/facebook_connect.php" link is very important. “onlogin =”top.location.href ='example.com / facebook_connect.php“链接非常重要。

Here my code: 这是我的代码:

<div id="fb-root"></div>
<script type="text/javascript" language="JavaScript" src="http://connect.facebook.net/de_DE/all.js#appId=myappid6&xfbml=1"></script>
<script type="text/javascript" language="JavaScript">  FB.init({
appId  : myappid,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml  : true  // parse XFBML
});
</script>


<fb:login-button scope="email, public_profile" size="xlarge" onlogin="top.location.href='example.com/facebook_connect.php'">Connect with Facebook</fb:login-button>

Thank you very much. 非常感谢你。

HTML: HTML:

<button id="loginBtn">Facebook Login</button>

JavaScript: JavaScript的:

document.getElementById('loginBtn').addEventListener('click', function() {
    //do the login
    FB.login(function(response) {
        if (response.authResponse) {
            //user just authorized your app
            top.location.href = 'example.com/facebook_connect.php';
        }
    }, {scope: 'email,public_profile', return_scopes: true});
}, false);

Source: http://www.devils-heaven.com/facebook-javascript-sdk-login/ 资料来源: http//www.devils-heaven.com/facebook-javascript-sdk-login/

Don´t use a-tags for this, it´s not a link. 不要为此使用a-tags,它不是链接。 You don´t need jQuery either, it´s just a simple click handler. 你也不需要jQuery,它只是一个简单的点击处理程序。 I suggest using the asynchronous way to load the JS SDK though. 我建议使用异步方式加载JS SDK。

I have used my custom button that hides default facebook login button. 我使用了隐藏默认Facebook登录按钮的自定义按钮。 See if it helps. 看看它是否有帮助。

<div class="img_container">
     <a class="btnFacebookLogin" href="#" role="button">
       <span class="img_icon icon_facebook" title="Facebook"></span>
       <div class="fb-login-button" onlogin="javascript:CallAfterLogin();" width="200" size="medium" autologoutlink="true" scope="user_education_history,user_birthday,friends_birthday,user_hometown,user_likes,email,user_checkins,user_groups,friends_groups,user_photos,user_events,user_location,user_relationships,user_relationship_details,user_religion_politics,user_website,user_work_history,user_interests,user_about_me"
           style="display: none;">
       </div>
     </a>
 </div>

JS JS

$(document).ready(function () {
    $('.btnFacebookLogin').on('click', function () {
        FB.login(function (response) { CallbackAfterLogin(response); }, { scope: 'email' });
    });
});

CSS CSS

.img_container
{
    display: inline-block;
    margin-right: 5px;
}

.img_icon
{
   display: block;
   width: 42px;
   height: 42px;
}


.icon_facebook
{
    background-image: url("../Images/Icons/social.png");
    background-position: 0 0;
    background-repeat: no-repeat;
    background-size: 172px 84px;
    height: 42px;
    width: 42px;
}

You can adjust the CSS according to your custom image. 您可以根据自定义图像调整CSS。 I allowed 'login with' option for multiple sites like Twitter, g+; 我允许在Twitter,g +等多个网站上“登录”选项; etc. So I used a single image that has all the necessary icons. 所以我使用了一个具有所有必要图标的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM