Facebook分享插件按钮没有分享权url
Facebook share plugin button does not share the right url
我的制作网站可以在这里找到:http://infinite-brushlands-3960.herokuapp.com/
我按照此处的说明设置了 javascript SDK:https://developers.facebook.com/docs/javascript/quickstart/v2.5
当用户点击"Share This Schedule"时,这段代码是运行:
$('#share_schedule').click(function(){
if ($('#share_url_ul').children().length >= 1){
$('#share_url_ul').empty();
}
// Take care of no classes case "You cannot share an empty schedule."
$.ajax({
method: "POST",
url: "/share/",
data: JSON.stringify(localStorage),
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(response){
var shared_url = document.createElement('a');
$(shared_url).css('display', 'block');
$(shared_url).attr('href', window.location.href + 'shared/' + response);
$(shared_url).attr('id', 'share_link');
shared_url.innerHTML = window.location.href + 'shared/' + response;
$('#share_url_ul').append(shared_url);
$('#fb_share_btn').attr('data-href', window.location.href + 'shared/' + response);
},
error: function(error){
console.log(error);
}
});
});
但是尽管将 facebook 按钮的 data-href 属性设置为我想分享的 url 的行(如此处所述 https://developers.facebook.com/docs/plugins/share-button ),但单击该按钮仍会将我的主页分享到facebook 而不是我在那里指定的 link。在浏览器检查器中检查按钮显示它确实具有正确的 url 作为 data-href 属性。
为什么插件分享的不正确url?
由于您要在 ajax 加载时更改按钮 url,更改属性后必须重新初始化 facebook 分享按钮。
尝试将此添加到成功回调的末尾
FB.XFBML.parse();
所以你应该有这样的东西
$('#share_schedule').click(function(){
if ($('#share_url_ul').children().length >= 1){
$('#share_url_ul').empty();
}
// Take care of no classes case "You cannot share an empty schedule."
$.ajax({
method: "POST",
url: "/share/",
data: JSON.stringify(localStorage),
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(response){
var shared_url = document.createElement('a');
$(shared_url).css('display', 'block');
$(shared_url).attr('href', window.location.href + 'shared/' + response);
$(shared_url).attr('id', 'share_link');
shared_url.innerHTML = window.location.href + 'shared/' + response;
$('#share_url_ul').append(shared_url);
$('#fb_share_btn').attr('data-href', window.location.href + 'shared/' + response);
FB.XFBML.parse();
},
error: function(error){
console.log(error);
}
});
});
我的制作网站可以在这里找到:http://infinite-brushlands-3960.herokuapp.com/
我按照此处的说明设置了 javascript SDK:https://developers.facebook.com/docs/javascript/quickstart/v2.5
当用户点击"Share This Schedule"时,这段代码是运行:
$('#share_schedule').click(function(){
if ($('#share_url_ul').children().length >= 1){
$('#share_url_ul').empty();
}
// Take care of no classes case "You cannot share an empty schedule."
$.ajax({
method: "POST",
url: "/share/",
data: JSON.stringify(localStorage),
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(response){
var shared_url = document.createElement('a');
$(shared_url).css('display', 'block');
$(shared_url).attr('href', window.location.href + 'shared/' + response);
$(shared_url).attr('id', 'share_link');
shared_url.innerHTML = window.location.href + 'shared/' + response;
$('#share_url_ul').append(shared_url);
$('#fb_share_btn').attr('data-href', window.location.href + 'shared/' + response);
},
error: function(error){
console.log(error);
}
});
});
但是尽管将 facebook 按钮的 data-href 属性设置为我想分享的 url 的行(如此处所述 https://developers.facebook.com/docs/plugins/share-button ),但单击该按钮仍会将我的主页分享到facebook 而不是我在那里指定的 link。在浏览器检查器中检查按钮显示它确实具有正确的 url 作为 data-href 属性。
为什么插件分享的不正确url?
由于您要在 ajax 加载时更改按钮 url,更改属性后必须重新初始化 facebook 分享按钮。
尝试将此添加到成功回调的末尾
FB.XFBML.parse();
所以你应该有这样的东西
$('#share_schedule').click(function(){
if ($('#share_url_ul').children().length >= 1){
$('#share_url_ul').empty();
}
// Take care of no classes case "You cannot share an empty schedule."
$.ajax({
method: "POST",
url: "/share/",
data: JSON.stringify(localStorage),
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(response){
var shared_url = document.createElement('a');
$(shared_url).css('display', 'block');
$(shared_url).attr('href', window.location.href + 'shared/' + response);
$(shared_url).attr('id', 'share_link');
shared_url.innerHTML = window.location.href + 'shared/' + response;
$('#share_url_ul').append(shared_url);
$('#fb_share_btn').attr('data-href', window.location.href + 'shared/' + response);
FB.XFBML.parse();
},
error: function(error){
console.log(error);
}
});
});