te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Facebook share via FB.ui - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Facebook share via FB.ui - Stack Overflow

programmeradmin2浏览0评论

I am trying to use the following code that I wrote based on the new documentation for showing a "post to feed" dialog (.ui) but i get the following error "ReferenceError: FB is not defined"

the code that i use is the simplest i can e up with:

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      status     : true,
      xfbml      : true
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = ".js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));


    FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: '/',
        picture: '.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });

Any ideas?

EDIT 1

and if i want to open the dialog when a user clicks on a link i would use jquery click event

$(".userActions a.facebook").click(function() {
   FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: '/',
        picture: '.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });
});

or have the FB.ui inside a function that accepts parameters and call this function e.g.

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      status     : true,
      xfbml      : true
    });

    // Code in here will run once FB has been initialised

    function FB_post_feed(method,name,link,picture,caption,description){
       FB.ui({
            method: method,
            name: name,
            link: link,
            picture: picture,
            caption: caption,
            description: description
        });
    }

};

(function(d, s, id){
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement(s); js.id = id;
   js.src = ".js";
   fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

and somewhere in the HTML

$(".userActions a.facebook").click(function() {
    FB_post_feed('feed','Facebook Dialogs','/','.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}

I am trying to use the following code that I wrote based on the new documentation for showing a "post to feed" dialog (https://developers.facebook./docs/javascript/reference/FB.ui) but i get the following error "ReferenceError: FB is not defined"

the code that i use is the simplest i can e up with:

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      status     : true,
      xfbml      : true
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "http://connect.facebook/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));


    FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook./docs/dialogs/',
        picture: 'http://fbrell./f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });

Any ideas?

EDIT 1

and if i want to open the dialog when a user clicks on a link i would use jquery click event

$(".userActions a.facebook").click(function() {
   FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook./docs/dialogs/',
        picture: 'http://fbrell./f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });
});

or have the FB.ui inside a function that accepts parameters and call this function e.g.

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      status     : true,
      xfbml      : true
    });

    // Code in here will run once FB has been initialised

    function FB_post_feed(method,name,link,picture,caption,description){
       FB.ui({
            method: method,
            name: name,
            link: link,
            picture: picture,
            caption: caption,
            description: description
        });
    }

};

(function(d, s, id){
   var js, fjs = d.getElementsByTagName(s)[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement(s); js.id = id;
   js.src = "http://connect.facebook/en_US/all.js";
   fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

and somewhere in the HTML

$(".userActions a.facebook").click(function() {
    FB_post_feed('feed','Facebook Dialogs','https://developers.facebook./docs/dialogs/','http://fbrell./f8.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}
Share Improve this question edited Mar 13, 2014 at 12:13 Andreas Traganidas asked Mar 13, 2014 at 11:50 Andreas TraganidasAndreas Traganidas 5131 gold badge7 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

Ok, there are a few conceptual mistakes you've done there.

First:

window.fbAsyncInit = function() {     
    FB.init({
        appId      : 'xxxxxxxx',
        status     : true,
        xfbml      : true
    });
};

(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "http://connect.facebook/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

FB.ui({
    method: 'feed',
    name: 'Facebook Dialogs',
    link: 'https://developers.facebook./docs/dialogs/',
    picture: 'http://fbrell./f8.jpg',
    caption: 'Reference Documentation',
    description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});

What is this supposed to do? Open a share dialog without user interaction? FB.ui must be called when Share Dialog is supposed to appear, not right after Facebook AsyncInit function.

And, also, facebook sdk will be started Asynchronously, as the name of the function suggests. It means FB will not be defined in the place you put the function.

Second:

window.fbAsyncInit = function() {     
    FB.init({
        appId      : 'xxxxxxxx',
        status     : true,
        xfbml      : true
    });

    // Code in here will run once FB has been initialised

    function FB_post_feed(method,name,link,picture,caption,description){
        FB.ui({
            method: method,
            name: name,
            link: link,
            picture: picture,
            caption: caption,
            description: description
        });
    }
};

FB_post_feed function, in this case, is a local function inside fbAsyncInit function. So, you don't have access to FB_post_feed function outside fbAsyncInit.

Besides, FB.init is asynchronous, which means FB will be undefined by the time FB_post_feed is being created.

Depending on how your HTML code is defined, and if this identifier is correct, the code

$(".userActions a.facebook").click(function() {
    FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook./docs/dialogs/',
        picture: 'http://fbrell./f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });
});

should work fine. However, just an advice: Classes are usually used to style something. It would be best-practice if you use the button(or "a" element) id instead.

I had the same problem and solved it by requesting the Facebook script by jQuery and then initializing it:

function FB_post_feed(method,name,link,picture,caption,description){
   FB.ui({
        method: method,
        name: name,
        link: link,
        picture: picture,
        caption: caption,
        description: description
    });
}


$(document).ready(function()
{
    $.getScript("http://connect.facebook/en_US/all.js#xfbml=1", function () {
        FB.init({ appId: 'xxxxxxxx', status: true, cookie: true, xfbml: true });
    });
});

Add this in your index.html

  <script type="text/javascript" src="https://connect.facebook/en_US/sdk.js"></script>
发布评论

评论列表(0)

  1. 暂无评论