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

时间:2017-02-23 03:41:29

标签: javascript

我有Twitter按钮工作,但Facebook按钮不是。 Facebook弹出窗口加载,但随后消失。 Chrome控制台中没有错误。

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

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);
}

1 个答案:

答案 0 :(得分:3)

您没有正确形成fbsLink变量。我看了here,发现u属性应该只是网址,title应该是分开的。这是一段固定的代码:

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