ahnbizcad
ahnbizcad

Reputation: 10797

Social buttons twitter facebook disappear when changing page with turbolinks

I'm using twitter and facebook social buttons without the need for SDKs or auths.

https://about.twitter.com/resources/buttons#tweet

and

https://developers.facebook.com/docs/plugins/share-button

When I first load the page or refresh, the tweet button and the share button show. But when I navigate via clicking links, the same social buttons don't show.

How do I get the scripts to load and play nice with turbolinks?

Upvotes: 1

Views: 248

Answers (1)

moeabdol
moeabdol

Reputation: 5049

It's due to turbolinks not reloading your javascript on page reload. This is an expected behavior of turbolinks; however, sometimes you need to reload third-party javascript as in your case.

Solution:

Disable turbolinks on links leading to the page containing javascript you need to reload. In your case links that lead to pages with your social buttons.

Example:

<%= link_to "the page containing your social buttons", social_page_path, "data-no-turbolink": true %>

Another Solution:

If your social buttons are binded to the page realod event then you can force turbolinks to reload them everytime. Use the complementary gem jquery.turbolinks Follow the installation instruction and you can force turbolinks to reload all javascript you want inside document.ready() funciton.

Example:

$(document).ready(function() {
    // your social buttons javascript code
});

This way you don't have to manually disable turbolinks on links leading to pages containing your social buttons.

Hope this helps.

Upvotes: 1

Related Questions