类型错误:不能设置属性'onclick'零
Error Uncaught TypeError: Cannot set property 'onclick' of null
我在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页面的不同部分。
相关文章:
- 如何通过它瞄准javascript元素's的onclick属性
- 如何在循环中设置onclick属性
- 具有多个功能的Onclick属性
- 使用 javascript 禁用 onclick 属性
- 在单击onclick属性后的按钮时执行f:setPropertyActionListener
- 如何在Rails表单标记中获取onclick属性
- 在django帮助文本字段中放置带有onClick属性的链接
- 创建选项onClick属性
- $(this) 在 onclick 属性中不起作用
- 如何在asp.net图像控件中使用OnClick属性来制作图像库
- 将.onclick属性中的文本替换为javascript
- 我可以't获取单选按钮上的onclick属性以执行javascript函数
- 为什么onclick属性不起作用
- 来自jquery的.append中img标记中的onclick属性
- javascript函数和html onclick属性
- 未调用通过 onclick 属性分配的处理程序
- onclick属性的引号过多的问题 - jQuery,HTML
- 使用 ajax 更改 onClick 属性不会触发新事件
- 在 onClick 属性中声明变量,并在 onClick 中使用多个命令
- 在输入表单标记中使用 onclick 属性时出现 JavaScript 未捕获的引用错误