This snippet is used to append a share link, specifically Twitter and Facebook links, on a blockquote to share the text inside it.

See live sample

Append Share Links on Blockquote Blocks Using JQuery

Before Starting

Make sure first that you have the following:
  • Facebook Developer App ID - For sharing in Facebook
  • Bit.ly API Key - For shortening the URL which is good for Twitter
  • JQuery

The Snippet

  1. // Get the current URL and strip all unnecessary parameters.
  2. $lurl = window.location.href;
  3. if ($lurl.indexOf("?")>-1){
  4. $lurl = $lurl.substr(0,$lurl.indexOf("?"));
  5. }
  6. if ($lurl.indexOf("#")>-1){
  7. $lurl = $lurl.substr(0,$lurl.indexOf("#"));
  8. }
  9.  
  10. // Shorten URL.
  11. $surl = "";
  12. get_short_url($lurl);
  13.  
  14. // Gets the shorten URL and append the social links.
  15. function get_short_url(long_url)
  16. {
  17. var url=long_url;
  18. var username="[REPLACE WITH BIT.LY USERNAME]";
  19. var key="[REPLACE WITH BIT.LY KEY]";
  20. $.ajax({
  21. url:"http://api.bit.ly/v3/shorten",
  22. data:{longUrl:url,apiKey:key,login:username},
  23. dataType:"jsonp",
  24. success:function(v)
  25. {
  26. var bit_url=v.data.url;
  27. $surl = bit_url;
  28. $("blockquote").each(function(){
  29. $originalHtml = $(this).html();
  30. $originalTxt = $(this).text();
  31. $(this).attr('id', "cbq" + cbq);
  32. $createShareButtons = createShareButtons($originalTxt, $originalHtml);
  33. $(this).append($createShareButtons);
  34. cbq++;
  35. });
  36.  
  37. }
  38. });
  39. }
  40.  
  41. // Create the Social links
  42. function createShareButtons(originalTxt, originalHtml){
  43. $shareButtons = $("<div></div>");
  44.  
  45. $shortenText = $.trim(originalTxt).substring(0, 90) + "..." + $surl;
  46.  
  47. $twitter = '<a class="shareWord" href="http://www.twitter.com/intent/tweet?via=pakiwariko&text=' + $shortenText.replace('"', '') + '" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,location=no,scrollbars=no,height=300,width=600');return false;" target="_blank">[Tweet] </a>';
  48. urlAtt = $lurl;
  49. caption = "[REPLACE WITH YOUR OWN CAPTION]";
  50. description = encodeURIComponent(originalTxt).replace('"', '');
  51. name = $title;
  52. appId = "[REPLACE WITH FB APP ID]";
  53.  
  54.  
  55. url = "https://www.facebook.com/dialog/feed?app_id="+ appId +"&link=" + urlAtt +
  56. "&caption=" + caption +
  57. "&description=" + description +
  58. "&name=" + name +
  59. "&redirect_uri=" + $lurl;
  60.  
  61. $fb = '<a class="shareWord" href="'+url+'" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=no,location=no,scrollbars=no,height=300,width=600');return false;" target="_blank">[Share] </a>';
  62.  
  63. $shareButtons.append($twitter);
  64. $shareButtons.append($fb);
  65. return $shareButtons;
  66. }

Recommendation

The aim of this snippet is to add social share links at a blockquote. By simply replacing the line $("blockquote").each(function(){ you can actually use this to append on divs and other HTML tags too that support $.append().



No comments :

Post a Comment