2013-05-21 50 views
0

林其在Facebook上的JavaScript错误“遗漏的类型错误:不能为空的设置属性的‘onClick’”行187和行187:错误遗漏的类型错误:无法设置属性的“onClick”空的

" 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> 

如果需要更多的信息,只是告诉我 我欣赏的帮助。 谢谢!

回答

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没有像上面的例子中你已经设置的按钮后,变量并在尝试使用它之前,然后更新您的问题在哪里出错。

使用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> 
+0

它说'按钮现在:null'。 –

+0

你是否过早调用updateButton函数?看起来没有ID为“fb-auth”的元素,所以你不能添加一个onclick事件。你的代码不会在fbAsyncInit被调用时显示,但是我的猜测是当内容尚未加载或者你期望的内容不存在时调用它。 – HMR

+0

对,没有呼叫fb-auth我只是改变了呼叫.. facebook login 但仍然是相同的..该电话是一个HTML不显示在网络上,我的意思是没有人会去那里.. –

0

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

相关问题