[英]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_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,而不是我在那里指定的鏈接。 在瀏覽器檢查器中檢查按鈕,表明它確實具有正確的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);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.