0

我在 Facebook Javascript “Uncaught TypeError: Cannot set property 'onclick' of null”第 187 行和第 187 行有错误:

" button.onclick = function() {"

所有的代码是:

<script type="text/javascript">
window.fbAsyncInit = function() {
  FB.init({ appId: <?php echo json_encode($this->getApiKey()) ?>, 
        status: true, 
        cookie: true,
        xfbml: true,
        oauth: true});
 FB.Canvas.setSize({ width: 640, height:1500 });
 //FB.Canvas.setAutoResize();
 function updateButton(response) {
    var button = document.getElementById('fb-auth');

    if (response.authResponse == 'null') {
      //user is already logged in and connected
      var userInfo = document.getElementById('user-info');
     FB.api('/me', function(response) {


      });

      button.onclick = function() {
        FB.logout(function(response) {
          var userInfo = document.getElementById('user-info');
          userInfo.innerHTML="";
    });
      };
    } else {
      //user is not connected to your app or logged out
      //button.innerHTML = 'Login';
      button.onclick = function() {
        FB.login(function(response) {
if(response.status=='connected') setLocation('<?php echo $this->getConnectUrl() ?>');
      if (response.authResponse) {
            FB.api('/me', function(response) {
          var userInfo = document.getElementById('user-info');


        });  

          } else {
            //user cancelled login or did not grant authorization
          }
         }, {scope:'email, user_birthday'});    
      }
    }
  }

  // run once with current status and whenever the status changes
  FB.getLoginStatus(updateButton);
  //FB.Event.subscribe('auth.statusChange', updateButton);  
};

(function() {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol 
    + '//connect.facebook.net/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
}());

</script>

如果需要更多信息,请告诉我,感谢您的帮助。谢谢!。

4

2 回答 2

0

可能是因为您的 JavaScript 不在页面底部,并且当它运行时,DOM 树还没有构建。构建元素后,尝试将代码放在 html 页面的不同部分。

于 2014-09-27T10:54:02.223 回答
0

快速查看您的代码表明当您尝试访问该按钮时该按钮不存在:

function updateButton(response) {
    var button = document.getElementById('fb-auth');
    //there is no element with id fb-auth
    console.log("button is now:",button);

请在设置按钮变量之后和尝试使用它之前将一些console.log 像上面的示例一样放在那里,然后更新您的问题到底哪里出错了。

使用带有 firebug 插件的 Chrome 或 Firefox 查看正确的 console.logs 并按 F12 打开开发工具(默认打开控制台选项卡)。

[更新]

我已经删除了引用 fb-auth 按钮的代码,所以如果该代码是旧的且未使用,那么以下应该可以工作:

<script type="text/javascript">
window.fbAsyncInit = function() {
  FB.init({ appId: <?php echo json_encode($this->getApiKey()) ?>, 
        status: true, 
        cookie: true,
        xfbml: true,
        oauth: true});
 FB.Canvas.setSize({ width: 640, height:1500 });
 function updateButton(response) {
    if (response.authResponse == 'null') {
     //user is already logged in and connected
     var userInfo = document.getElementById('user-info');
     FB.api('/me', function(response) {
     });
    } else {
  // run once with current status and whenever the status changes
  FB.getLoginStatus(updateButton);
  //FB.Event.subscribe('auth.statusChange', updateButton);  
};

(function() {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol 
    + '//connect.facebook.net/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
}());
</script>
于 2013-05-21T05:26:33.723 回答