[英]Loading Facebook SDK on click
Obviously, some people are not happy with websites using a Facebook plugin for they don't want to be tracked in what they do when they're not on Facebook. 显然,有些人对使用Facebook插件的网站不满意,因为当他们不在Facebook上时,他们不想被追踪他们所做的事情。 There's Shariff , which looks like a fair approach: The Facebook plugin isn't loaded until the user clicks the Facebook button (here's a demo ).
Shariff是一种看起来很公平的方法:在用户单击Facebook按钮之前,Facebook插件不会加载(这是一个演示 )。
I'm trying to do the same (using the JavaScript SDK). 我正在尝试做同样的事情(使用JavaScript SDK)。 However, I blatantly fail in loading the SDK dynamically.
但是,我公然无法动态加载SDK。 I didn't get any error messages either (yes, I did insert the app id).
我也没有收到任何错误消息(是的,我确实插入了应用程序ID)。
Any ideas? 有任何想法吗? The code below was taken from the API docs .
以下代码取自API文档 。
<html>
<body>
<script type="text/javascript">
window.onload = function() {
var e = document.getElementById('share_on_fb_link');
e.onclick = share_on_fb;
function share_on_fb() {
// Code from https://developers.facebook.com/docs/javascript/quickstart/v2.2
// Basic Setup
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
};
(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/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Using the SDK to trigger a Share dialog
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/'
}, function(response){});
return false;
}
}
</script>
<a id="share_on_fb_link" href="#">Link</a>
</body>
</html>
I finally came up with a working solution. 我终于想出了一个可行的解决方案。 Firstly, you have to call
FB.ui
after FB.init
. 首先,你必须调用
FB.ui
后FB.init
。 But according to https://stackoverflow.com/a/3549043/745266 this won't solve the problem completely - FB.init
is doing an asynchronous request to facebook, so calling FB.ui
must be delayed until Facebook is fully setup. 但是根据https://stackoverflow.com/a/3549043/745266,这无法完全解决问题
FB.init
向Facebook执行异步请求,因此必须延迟调用FB.ui
直到Facebook完全设置。 The following code should work: 下面的代码应该工作:
window.onload = function() {
var e = document.getElementById('share_on_fb_link');
e.onclick = share_on_fb;
function share_on_fb() {
// Code from https://developers.facebook.com/docs/javascript/quickstart/v2.2
// Basic Setup
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
// Delay FB.ui code until Facebook is fully initialized
FB.getLoginStatus(function(response){
// Using the SDK to trigger a Share dialog
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/'
}, function(response){});
});
};
(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/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.