简体   繁体   English

具有动态URL和标题的Twitter和Facebook自定义共享按钮

[英]Twitter & Facebook Custom Share Buttons With Dynamic URL & Title

I have the Twitter button working, but the Facebook button isn't. 我有Twitter按钮工作,但Facebook按钮不是。 The Facebook popup loads, but then it disappears. Facebook弹出窗口加载,但随后消失。 There are no errors in the Chrome console. Chrome控制台中没有错误。

HTML HTML

<i onclick="fbs_click();" class="fa fa-facebook article-tab-sub"></i>
<i onclick="twr_click();" class="fa fa-twitter article-tab-sub"></i>

Javascript 使用Javascript

function twr_click() {
    var twtTitle = document.title;
    var twtUrl = location.href;
    var maxLength = 140 - (twtUrl.length + 1);
    if (twtTitle.length > maxLength) {
        twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
    }
    var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
    var x = screen.width/2 - 280/2; var y = screen.height/2 - 280/2; window.open(twtLink, '','height=280,width=280,left='+x+',top='+y);
}
function fbs_click() {
    var fbsTitle = document.title;
    var fbsUrl = location.href;
    var maxLength = 140 - (fbsUrl.length + 1);
    if (fbsTitle.length > maxLength) {
        fbsTitle = fbsTitle.substr(0, (maxLength - 3)) + '...';
    }
    var fbsLink = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(fbsTitle + ' ' + fbsUrl);
    var x = screen.width/2 - 280/2; var y = screen.height/2 - 280/2; window.open(fbsLink, '','height=280,width=280,left='+x+',top='+y);
}

You're not forming the fbsLink variable correctly. 您没有正确形成fbsLink变量。 I looked here and found that the u attribute should only be the url and that the title should be separate. 我看了一下 ,发现u属性应该只是url, title应该是分开的。 Here's a fixed line of code: 这是一段固定的代码:

var fbsLink = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(fbsUrl) + '&title=' + encodeURIComponent(fbsTitle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM