[英]Facebook share plugin button does not share the right url
My production site can be found here: http://infinite-brushlands-3960.herokuapp.com/ 我的生产站点可以在这里找到: http : //infinite-brushlands-3960.herokuapp.com/
I have the javascript SDK set up as instructed here: https://developers.facebook.com/docs/javascript/quickstart/v2.5 我已按照此处的指示设置了javascript SDK: https : //developers.facebook.com/docs/javascript/quickstart/v2.5
When the user clicks "Share This Schedule", this code is run: 当用户单击“共享此计划”时,将运行以下代码:
$('#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);
}
});
});
But despite the line that sets the facebook button's data-href attribute to the url that I want to share (as described here https://developers.facebook.com/docs/plugins/share-button ), clicking the button still shares my home page to facebook instead of the link I specified there. 但是尽管设置了将facebook按钮的data-href属性设置为我要共享的url的行(如此处https://developers.facebook.com/docs/plugins/share-button所述 ),但单击该按钮仍会共享我的主页到facebook,而不是我在那里指定的链接。 Inspecting the button in the browser inspector show that it indeed has the correct url as the data-href attribute.
在浏览器检查器中检查按钮,表明它确实具有正确的URL作为data-href属性。
Why isn't the plugin sharing the correct url? 为什么插件不共享正确的URL?
Since you are changing the button url on ajax load, you have to re-initialize the facebook share button after changing the attributes. 由于您是在ajax加载中更改按钮URL,因此必须在更改属性后重新初始化facebook共享按钮。
Try adding this to the end of success callback 尝试将其添加到成功回调的末尾
FB.XFBML.parse();
So you should have something like 所以你应该有类似
$('#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.