Social share buttons not loading when changing page in rails

I am using social mailing buttons for twitter and facebook. The Javascript that turns these divs into buttons doesn't show up unless I load the first page or refresh the page. In other words, if I click the link, I'm pretty sure turbolinks is not running the script to display the buttons.

How can I change the javascript that twitter and facebook provides to include divs or links in shared 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 change the javascript.

FYI, I have a jquery-turbolinks gem so I can do it without problem

example.js.coffee

$(document).ready ->
  /* do stuff */

      

+3


source to share





All Articles