类型错误:不能设置属性'onclick'零

Error Uncaught TypeError: Cannot set property 'onclick' of null

本文关键字:onclick 属性 错误 不能 设置 类型      更新时间:2023-09-26

我在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>

如果需要更多的信息就告诉我谢谢你的帮助。谢谢!。

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

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,然后更新你的问题,到底是哪里出了问题

使用Chrome或Firefox与firebug插件来查看适当的控制台。按F12打开dev tools(默认打开console选项卡)。

(更新)

我已经删除了引用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>

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