【问题标题】:Error Uncaught TypeError: Cannot set property 'onclick' of null错误未捕获类型错误:无法设置属性“onclick”为空
【发布时间】:2013-05-21 05:19:11
【问题描述】:

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

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

【问题讨论】:

    标签: javascript facebook authentication


    【解决方案1】:

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

    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 插件查看正确的 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>
    

    【讨论】:

    • 上面写着button is now: null 。
    • 您是否过早调用 updateButton 函数?似乎没有 ID 为“fb-auth”的元素,因此您无法向其添加 onclick 事件。您的代码在调用 fbAsyncInit 时未显示,但我猜它是在尚未加载内容或您期望的内容不存在时调用的。
    • 对,没有调用 fb-auth 我只是将调用更改为.. getSkinUrl('images/flogin.jpg') ?>" id="facebook" alt="facebook login" /> 但还是一样......那个电话是一个没有在网络上显示的 html,我的意思是没有人会去那里..
    • 你发布的html代码;那是你页面的一部分吗?如果是,则 document.getElementById("fb-auth") 不应返回 null ,除非您的代码和 href 位于不同的框架中。
    • 是的,它是我页面的一部分,但就像我说的那样,从不使用网络是旧登录。我该如何解决?错误的代码在prototype.js上,代码“..." 在旧版本中登录。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      相关资源
      最近更新 更多