Facebook api正在打开两个弹出窗口

时间:2012-05-17 09:39:13

标签: jquery facebook

我在facebook api工作,但发生的事情是当我点击“登录facebook”时,会打开两个弹出窗口,而不是一个。

如果用户 已登录Facebook ,点击该按钮就会打开一个弹出窗口并在几秒钟内< strong>通过检查会话返回,我的网站完美,但以防用户未登录 ,在这种情况下 两个弹出窗口出现 ,现在当用户输入凭据正确并且按下输入在弹出的窗口中,它会关闭,但由于第二个(已经存在)窗口,我的脚本不起作用,但会话变量保存的方式 userId (实际上我想要的)(我知道这件事,因为当我关闭不需要的弹出窗口后刷新我的网站索引页面时,我会正常访问我的个人资料页面。)

我也提到this page来解决但没有用。请打开一些灯只打开一个弹出窗口。

我使用此代码:

<script src="http://connect.facebook.net/en_US/all.js"></script>



<!-- Start of facebook login -->
<div id="fb-root"></div>
<fb:login-button size="large" id='login'">
      Login with Facebook
</fb:login-button>
<script>
  // initialize the library with the API key
  window.fbAsyncInit = function() {
      try{
        FB.init({
            apiKey      : 'API KEY',
            status      : true, 
            cookie      : true,
            xfbml       : true,
            oauth       : true
        }); 

        FB.getLoginStatus(function(response){
            loginHandler(); 
        });
      }catch(error){}
  };  
    function loginHandler(response,fail)
    {
        try{
            if(response && response.authResponse){
                FB.api('/me', function(response,fail) {

                    var email=response.email;
                    var firstName=response.first_name;
                    var lastName=response.last_name;
                    var gender=response.gender;
                    var dob=response.birthday;
                    var remainLogin=$('#remainLogin').val();

                    $('#login').attr("disabled", true);

                    $.ajax({
                        url: "loginViaFacebook",
                        type: "POST",
                        data: {email:email,firstName:firstName,lastName:lastName,gender:gender,dob:dob,remainLogin:remainLogin},
                        success: function (data) {

                            if (data.indexOf("true") != -1){ 
                                location.href = 'MainHome.jsp';
                            }
                            else 
                                jAlert('Email not registered ... ','Email not found',ERROR);

                            $('#login').attr("disabled", false);
                        },
                        error: function(xhr, ajaxOptions, thrownError){
                            if(xhr.status==404)//Wrong url
                                jAlert('Check the url','ERROR',ERROR);
                            else if(xhr.status==500)//Exception in server side coding
                                jAlert('Exception in server coding, check your email or try one more time','ERROR',ERROR);
                            else if(xhr.status==408)//timeout
                                jAlert('Server taking time to respond','ERROR',ERROR);
                            else 
                                jAlert('Some error occured ' + thrownError,'ERROR',ERROR);

                            $('#login').attr("disabled", false);
                        },
                        timeout : 5000
                    });

                });
            }
        }catch(error){alert(error);}
    }
    var popUp;
    $('#login').bind('click', function(response) {
        try{
            FB.login(loginHandler, {scope: 'email,user_birthday'});
        }catch(error){}
    });

    $('#logout').bind('click', function() {
        try{
            FB.logout(function(response){});
        }catch(error){}
      });


  </script>


<!-- End of facebook login -->

1 个答案:

答案 0 :(得分:0)

你在“window.fbAsyncInit”中调用“loginHandler”以及点击按钮,在你的fbinit方法中替换它并尝试

  FB.getLoginStatus(function(response){
  if (response.status === 'connected') {
           loginHandler();

        }             
   });