2016-12-19 36 views
0

我想嵌入Facebook登录功能,授予用户访问我的网站。我的问题是,脚本自动触发登录(和重定向到“/views/test_view.php”),但我只希望当有人点击Facebook按钮时触发此。facebook登录web autotriggering js脚本

正如你可以在代码中看到的,我尝试添加一个点击事件并覆盖登录按钮中的标准功能,但它不起作用。 有关如何将脚本更改为仅在“单击”时执行的任何建议将不胜感激!谢谢!

<script> 
    function statusChangeCallback(response) { 
     console.log('statusChangeCallback'); 
     console.log(response); 
     // The response object is returned with a status field that lets the 
     // app know the current login status of the person. 
     // Full docs on the response object can be found in the documentation 
     // for FB.getLoginStatus(). 
     if (response.status === 'connected') { 
      // Logged into your app and Facebook. 
      testAPI(); 
     } else if (response.status === 'not_authorized') { 
      // The person is logged into Facebook, but not your app. 
      document.getElementById('status').innerHTML = 'Please log ' + 
       'into this app.'; 
     } else { 
      // The person is not logged into Facebook, so we're not sure if 
      // they are logged into this app or not. 
      document.getElementById('status').innerHTML = 'Please log ' + 
       'into Facebook.'; 
     } 
    } 

    function checkLoginState() { 
     FB.getLoginStatus(function(response) { 
      statusChangeCallback(response); 
     }); 
    } 


    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '1783721038549063', 
      xfbml: true, 
      version: 'v2.6' 
     }); 

     FB.getLoginStatus(function(response) { 
      statusChangeCallback(response); 
     }); 

    }; 

    (function(d, s, id) { 
     var js, fjs = d.getElementsByTagName(s)[0]; 
     if (d.getElementById(id)) { 
      return; 
     } 
     js = d.createElement(s); 
     js.id = id; 
     js.src = "//connect.facebook.net/en_US/sdk.js"; 
     fjs.parentNode.insertBefore(js, fjs); 
    }(document, 'script', 'facebook-jssdk')); 

    function testAPI() { 
     window.location.href = "/views/test_view.php"; 
    }; 

    window.onload = function() { 
     document.getElementById("facebook").onclick = function checkLoginState() {}; 

     function wait() { 
      console.log("wait called"); 
     } 
    } 
</script> 

<fb:login-button id="facebook" auto_logout_link=true scope="public_profile,email" size=large onlogin="wait():"> 
</fb:login-button> 

<div id="status"> 
</div> 
+1

看起来你'testAPI'功能,这样做一切,而你是当用户登录到Facebook并连接到您的应用程序之前调用它。 – CBroe

回答

0

找到了解决办法:

第一次加载FB API 他们包裹在onclick听众

window.fbAsyncInit = function() { 
         FB.init({ 
          appId  : '1783721038549063', 
          xfbml  : true, 
          version : 'v2.6' 
         }); 
        }; 

        (function(d, s, id){ 
         var js, fjs = d.getElementsByTagName(s)[0]; 
         if (d.getElementById(id)) {return;} 
         js = d.createElement(s); js.id = id; 
         js.src = "//connect.facebook.net/en_US/sdk.js"; 
         fjs.parentNode.insertBefore(js, fjs); 
         }(document, 'script', 'facebook-jssdk')); 

        window.onload = function() { 
        document.getElementById("facebook").onclick = function checkLoginState() { 
         FB.getLoginStatus(function(response) { 
         statusChangeCallback(response); 
         }); 
        }; 
        } 

        function statusChangeCallback(response) 
        { 
         console.log('statusChangeCallback'); 
         console.log(response); 
         // The response object is returned with a status field that lets the 
         // app know the current login status of the person. 
         // Full docs on the response object can be found in the documentation 
         // for FB.getLoginStatus(). 
         if (response.status === 'connected') 
         { 
          // Logged into your app and Facebook. 
          testAPI(); 
         } 
         else if (response.status === 'not_authorized') 
         { 
         // The person is logged into Facebook, but not your app. 
          FB.login(function(response) 
          { 
          document.getElementById('status').innerHTML = 'Please log ' + 
          'into this app.'; 
          }); 
         } 

         else 
         { 
          // The person is not logged into Facebook, so we're not sure if 
          // they are logged into this app or not. 
          FB.login(function(response){ 
          document.getElementById('status').innerHTML = 'Please log ' + 
          'into Facebook.'; 
          }); 
         } 
        } 

        function testAPI() { 
         window.location.href= "/views/landing_view.php"; 
        } 

        </script>