How to add facebook share button on my website?

前端 未结 7 1625
情书的邮戳
情书的邮戳 2020-12-02 04:09

I have this code that suppose to work, but doesn\'t work. If this helps you in anyway that would be great.



        
7条回答
  •  日久生厌
    2020-12-02 04:34

    For Facebook share with an image without an API and using a # to deep link into a sub page, the trick was to share the image as picture=

    The variable mainUrl would be http://yoururl.com/

    var d1 = $('.targ .t1').text();
    var d2 = $('.targ .t2').text();
    var d3 = $('.targ .t3').text();
    var d4 = $('.targ .t4').text();
    var descript_ = d1 + ' ' + d2 + ' ' + d3 + ' ' + d4;
    var descript = encodeURIComponent(descript_);
    
    var imgUrl_ = 'path/to/mypic_'+id+'.jpg';
    var imgUrl = mainUrl + encodeURIComponent(imgUrl_);
    
    var shareLink = mainUrl + encodeURIComponent('mypage.html#' + id);
    
    var fbShareLink = shareLink + '&picture=' + imgUrl + '&description=' + descript;
    var twShareLink = 'text=' + descript + '&url=' + shareLink;
    
    // facebook
    $(".my-btn .facebook").off("tap click").on("tap click",function(){
      var fbpopup = window.open("https://www.facebook.com/sharer/sharer.php?u=" + fbShareLink, "pop", "width=600, height=400, scrollbars=no");
      return false;
    });
    
    // twitter
    $(".my-btn .twitter").off("tap click").on("tap click",function(){
      var twpopup = window.open("http://twitter.com/intent/tweet?" + twShareLink , "pop", "width=600, height=400, scrollbars=no");
      return false;
    });
    

提交回复
热议问题