3

I use the social share buttons for twitter and facebook. The javascript that turns those divs into buttons dont' show up unless it's the first page I load on, or unless I refresh the page. In other words, if I click on a link, I'm pretty sure turbolinks doesn't trigger the script to show the buttons.

How do I modify the javascript that twitter and facebook provide to turn the divs or linkes into share buttons?

Twitter.js

!function(d,s,id){
  var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
  if(!d.getElementById(id)){
    js=d.createElement(s);js.id=id;
    js.src=p+'://platform.twitter.com/widgets.js';
    fjs.parentNode.insertBefore(js,fjs);
  }
}(document, 'script', 'twitter-wjs');

show.html.slim

= link_to "https://twitter.com/share", class: "twitter-share-button", data: { count: "none", url: hack_url(hack), text: hack.body, via: "GhettoLifeHack_", hashtags: "ghettolifehack" } do

Same story, but here are the details for facebook anyway.

facebook.js.coffee

window.fbAsyncInit = ->
  FB.init
    appId: <%= ENV['FACEBOOK_APP_ID'] %>
    xfbml: true
    version: "v2.1"
  return

((d, s, id) ->
  app_id = <%= ENV['FACEBOOK_APP_ID'] %>

  js = undefined
  fjs = d.getElementsByTagName(s)[0]
  return  if d.getElementById(id)
  js = d.createElement(s)
  js.id = id
  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=#{app_id}&version=v2.0"

  fjs.parentNode.insertBefore js, fjs
  return  
) document, "script", "facebook-jssdk"

show.html.slim

.fb-share-button[ data-href="#{hacks_url(hack)}" data-layout="button" ]

I'm pretty sure I need to modify the javascript.

FYI, I have jquery-turbolinks gem, so I can do this with no problem

example.js.coffee

$(document).ready ->
  /* do stuff */
ahnbizcad
  • 10,491
  • 9
  • 59
  • 85

0 Answers0