使用fb.ui?</textarea>从<textarea>发布消息给facebook

时间:2011-03-14 23:12:00

标签: javascript api post facebook

我已经读过:

Using Facebook Graph to simply post a wall message with just javascript

我正在寻找类似的东西,

l>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>
  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js"></script>
  <script>
     FB.init({ 
        appId:'147622691*******', cookie:true, 
        status:true, xfbml:true 
     });

      FB.ui({ method: 'feed', 
        message: 'Facebook for Websites is super-cool'});

  </script>
  <fb:login-button perms="read_stream,publish_stream">Login with Facebook</fb:login-button>


<div id="content"></div>
<form>    <textarea name="text" id="comment"></textarea> 
<a href="#" id="submit_comment">submit</a></form>


</body>

但不是发布'Facebook for Websites非常酷'...我想发布将在我的textarea中写入id = comment的文本。

所以用户进入网站,写入文本区域,按下触发对话框的按钮,对话框显示文本区域的文本。

我猜我需要添加一些额外的javascipt和一些getelementbyid和一个onclick事件,但我的javascript非常有限。

解决方案或建议将不胜感激.~

谢谢

1 个答案:

答案 0 :(得分:1)

这是一个完整的工作示例:

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>JS-SDK FB.ui Test</title>
  </head>
  <body>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : 'XXXXXXXXXXXX',
          status  : true,
          cookie  : true,
          xfbml   : true
        });

        FB.Event.subscribe('auth.login', gotStatus);

        FB.Event.subscribe('auth.sessionChange', gotStatus);
      };

      function post(form) {
        if(form.comment.value.length) {
            FB.getLoginStatus(function(resp) {
                if (resp.session) {
                    FB.ui(
                        {
                            method: 'feed',
                            message: form.comment.value
                        },
                        function(response) {
                            if (response && response.post_id) {
                                alert('Post was published.');
                            } else {
                                alert('Post was not published.');
                            }
                        }
                    );
                } else {
                    alert("Please Login!");
                }
            });
        } else {
            alert("You need to write something first!")
        }
        return false;
      }
        function gotStatus(response) {
            if(response.session)
                document.getElementById('login-btn').disabled = true;
            else
                document.getElementById('login-btn').disabled = false;
        }
      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
    <input  type="button"
            id="login-btn"
            value="Publish Stream"
            onclick="FB.login(gotStatus, {perms: 'publish_stream'})" />
    <form onSubmit="return post(this);">
        <textarea name="text" id="comment"></textarea>
        <input type="submit" value="Submit" />
    </form>
  </body>
</html>