开发者

How to use facebook publish stream method in a '.js' file

开发者 https://www.devze.com 2023-03-31 22:36 出处:网络
I am using bellow code which is in a \'.php\' file, to publish stream. <script src=\"http://connect.facebook.net/en_US/all.js\"></script>

I am using bellow code which is in a '.php' file, to publish stream.

<script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init( 
      {
        appId  : 'MY_APP_ID',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
      });

      var publish = {
        method: 'stream.publish',
        message: '',
        picture : 'http://www.associationregion.org/images/stories/slides开发者_高级运维/Upload.png',
        link : 'http://apps.facebook.com/puzzlegts/',
        name: 'Go to image',
        caption: 'taged you in a photo',
        description: 'test 2',
        actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
        };
        FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});

    </script>

Now I have a javascript file named 'publish.js' and I want to add publish stream method to a function in my 'publish.js' file. Can anyone please tell me how to do this?

Problem arise since I can't use '<script>' tag in a '.js' file.Therefore I can't include bellow code.

<script src="http://connect.facebook.net/en_US/all.js"></script>  

Or can anyone please post the suitable code for a '.js' file


Add this to your page

<script src="publish.js"></script>

and add this to your publish.js

  FB.init( 
  {
    appId  : '266000346762008',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });

  var publish = {
    method: 'stream.publish',
    message: '',
    picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
    link : 'http://apps.facebook.com/puzzlegts/',
    name: 'Go to image',
    caption: 'taged you in a photo',
    description: 'test 2',
    actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
    };
    FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});

(without script tags)


You have add it programmatically then or just use the asynchronous loading:

window.fbAsyncInit = function() {
    FB.init({
        appId  : 'MY_APP_ID',
        status : true, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : true  // parse XFBML
    });
    var publish = {
        method: 'stream.publish',
        message: '',
        picture : 'http://www.associationregion.org/images/stories/slides/Upload.png',
        link : 'http://apps.facebook.com/puzzlegts/',
        name: 'Go to image',
        caption: 'taged you in a photo',
        description: 'test 2',
        actions : { name : 'Go to app', link : 'http://apps.facebook.com/puzzlegts/'}
    };
    FB.api('/me/feed', 'POST', publish, function(response) {alert("posted");});
};
(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);
}());

Notes:

  • You need to insure that you have the fb-root div is in your markup
  • this will only fire once the library all.js is fully loaded
0

精彩评论

暂无评论...
验证码 换一张
取 消