我想在我的Facebook墙上发布HTML id的值作为消息。发布到Facebook工作,但FB最终编写未定义为值传入消息:我使用var word = document.getElementById("fb-root").value;
来获取值,但fb.api
正在将值转换为undefined。
请参阅以下代码:
<html>
<head>
<title>My Facebook Login Page</title>
<script type="text/javascript">
function postToWall() {
var word = document.getElementById("fb-root").value;
var params = {};
params['message'] = word;
params['name'] = 'test';
params['description'] = 'test';
params['link'] = 'asas.com';
params['picture'] = 'http://3.bp.blogspot.com/--jbgq-gytJQ/URaJHK_93LI/AAAAAAAAAF0/SkyoK7H3r7U/s1600/Simple+Apple.png';
params['caption'] = 'test';
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert(word);
}
});
}
</script>
<div id="fb-root">test</div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'test',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</head>
<body>
<div id="fb-root">tests</div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:<fbapp-id>, cookie:true,
status:true, xfbml:true
});
</script>
<fb:login-button perms="publish_stream">Login with Facebook</fb:login-button>
<a href="javascript:postToWall()">Post To Wall</a>
</body>
</html>
答案 0 :(得分:0)
检查代码后(它搞砸了)你需要注意这些要点,
一切都重复
在HTML中,您不能拥有多个id
元素,这里有2个元素id
fb-root
<div id="fb-root">test</div>
您正在加载JS-SDK两次,一次是异步的;
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
和其他同步(使用<script>
)
<script src="http://connect.facebook.net/en_US/all.js"></script>
您致电FB.init()
两次
您将fb-root
元素中的一个放入head
标记中,这是HTML中的罪行;)
<div id="fb-root"></div>
从DOM中移除,它就像一个虚拟元素,但对Facebook很重要,不是你或我,你应该总是离开它而不是使用它,而是创建与“id
不同myWord
的新元素
<div id="myWord">my word</div>
div
虽然<html>
<head>
<title>My Facebook Login Page</title>
<script type="text/javascript">
function postToWall() {
var word = document.getElementById("myWord").value;
var params = {};
params['message'] = word;
params['name'] = 'test';
params['description'] = 'test';
params['link'] = 'asas.com';
params['picture'] = 'http://3.bp.blogspot.com/--jbgq-gytJQ/URaJHK_93LI/AAAAAAAAAF0/SkyoK7H3r7U/s1600/Simple+Apple.png';
params['caption'] = 'test';
FB.api('/me/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert(word);
}
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '{App_id}',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
</head>
<body>
<div id="fb-root"></div>
<fb:login-button perms="publish_stream">Login with Facebook</fb:login-button>
<a href="javascript:postToWall()">Post To Wall</a>
</body>
</html>
不是内联元素,但却是一种不好的做法。
这是您清理过的代码;
{{1}}
答案 1 :(得分:0)
的document.getElementById( “myWord” 的)。值
需要更改为
的document.getElementById( “myWord” 的)。innerHTML的