Console.log 加载(显示)异步 Facebook Like 按钮后的内容
Console.log something once async Facebook Like button is loaded (displayed)
我在我的页面上使用简单的赞按钮:
<div id="fb-root"></div>
<script>(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 = "//connect.facebook.net/pl_PL/sdk.js#xfbml=1&version=v2.4";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
和我的HTML
<div class="fb-like"
data-href="https://www.facebook.com/link_to_sample_fanpage"
data-layout="button_count"
data-action="like"
data-show-faces="false"
data-share="false">
</div>
一切正常。按钮是异步加载的,这很酷。我想要实现的是执行特定的操作,假设加载按钮后 console.log('async button loaded)
。
初始化时的回调之类的东西?我必须使用一些 FB API 吗?
JS SDK允许您订阅某些事件,其中之一是xfbml.render
:
Fired when FB.XFBML.parse() completes. This indicates that all of the social plugins on the page have been loaded.
https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.4
(您不需要显式调用 FB.XFBML.parse
,它也会在初始加载时为 SDK 解析的社交插件触发。)
谢谢 CBroe。您的回答非常有帮助。我会接受它,但为了添加一些有价值的东西,我还将在下面粘贴我的最终解决方案。
Inside window.fbAsyncInit
我添加了名为 social_finished_rendering
的自定义事件。
FB.Event.subscribe('xfbml.render', social_finished_rendering);
然后在后面的代码中
var social_finished_rendering = function() {
// console log whatever
};
不,加载异步“点赞”按钮后,我能够执行 DOM 操作。谢谢 CBroe!
我在我的页面上使用简单的赞按钮:
<div id="fb-root"></div>
<script>(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 = "//connect.facebook.net/pl_PL/sdk.js#xfbml=1&version=v2.4";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
和我的HTML
<div class="fb-like"
data-href="https://www.facebook.com/link_to_sample_fanpage"
data-layout="button_count"
data-action="like"
data-show-faces="false"
data-share="false">
</div>
一切正常。按钮是异步加载的,这很酷。我想要实现的是执行特定的操作,假设加载按钮后 console.log('async button loaded)
。
初始化时的回调之类的东西?我必须使用一些 FB API 吗?
JS SDK允许您订阅某些事件,其中之一是xfbml.render
:
Fired when FB.XFBML.parse() completes. This indicates that all of the social plugins on the page have been loaded.
https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.4
(您不需要显式调用 FB.XFBML.parse
,它也会在初始加载时为 SDK 解析的社交插件触发。)
谢谢 CBroe。您的回答非常有帮助。我会接受它,但为了添加一些有价值的东西,我还将在下面粘贴我的最终解决方案。
Inside window.fbAsyncInit
我添加了名为 social_finished_rendering
的自定义事件。
FB.Event.subscribe('xfbml.render', social_finished_rendering);
然后在后面的代码中
var social_finished_rendering = function() {
// console log whatever
};
不,加载异步“点赞”按钮后,我能够执行 DOM 操作。谢谢 CBroe!