Since the module doesn't use standard Drupal practices (JavaScript behaviors), if the ShareThis JavaScript is loaded by an Ajax call (a Colorbox view of a node, for instance), the ShareThis buttons will only appear on the node loaded in the very first Colorbox Ajax call, until the entire page is reloaded.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jvalencia’s picture

Can you be a little more specific? I'm not too familiar with either Colorbox views or ajax in Drupal.

adamdicarlo’s picture

JavaScript binding/activation logic is supposed to be run in a Drupal behavior. Also, the ShareThis JavaScript requires calling stButtons.locateElements() after Ajax has placed new content in a page (calling stLight.options() again doesn't do anything after the first time).

I wrote my own ShareThis integration module the other day -- here's the JavaScript I used to invoke ShareThis that doesn't have the problem of activating the buttons for the node in only the first Ajax call:

;(function($) {
  Drupal.behaviors.osShareThis = {
    attach: function(context, settings) {
      stLight.options({
        publisher: settings.publisherId
      });
      // In case we're being attached after new content was placed via Ajax,
      // force ShareThis to create the new buttons.
      stButtons.locateElements();
    }
  };
})(jQuery);
jvalencia’s picture

So, I was trying to read the docs and get a handle on this, but I'm having trouble understanding.

Does this attach behavior get called automatically on each Ajax call?

So to fix this I would keep the:
drupal_add_js() in sharethis.module

and then add:
Drupal.behaviors.... with stButtons.locateElements(); in some .js file?

So that should take care of it?

alanburke’s picture

Version: 7.x-1.0 » 7.x-2.x-dev
Status: Active » Needs review
FileSize
3.28 KB

I'm still finding my way around the Drupal JS world.
So this code is rough and ready, but it does work.

I'm adding the code using hook_init - which is probably wrong, but it works.

If nothing else, this patch will maybe give an idea of how to approach the issue.

@adamdicarlo - thanks - you're a lifesaver!

TravisWeihermuller’s picture

I tried the patch above (comment #4) and it did not work for me. I'm using Sharethis buttons displayed in the links of a node then displayed in a view that uses AJAX (via Views Load More Module). The links work on the first load of the view then once AJAX is called to load more rows, the sharethis buttons disappear. I'd be happy to provide more information an test additional patches if need be.

alanburke’s picture

I'm not sure its the same issue - in my case the buttons were displayed, but the Javascript didn't work.

andyg5000’s picture

I had a problem where including blocks via ajax that used ShareThis did not function properly. Event handlers were broken and sometimes the spans were not updated with the appropriate links and images. The patch supplied required the module path to match the path of @alanburke's and there were a few things missing or in the wrong place. Here's an updated patch to fix this problem. I've added some javascript in hook_init, but there may be a better place to include this. The new sharethis.js is included in the .info file and could probably be combined with one of the other js files in the module.

billp44ruby’s picture

I use a lot of Views (both in blocks and pages) and just implemented ShareThis. When you setup a View with "Use Ajax" set to "yes", ShareThis links only appear on the initial node and are not displayed on nodes subsequently called via Ajax. If you set "Use Ajax" to "no", ShareThis links appear on all nodes. An easy workaround for the time being, but I would like to enable Ajax calls on these Views eventually.

tea.time’s picture

I have been digging into this issue and figured out the following.

As far as I can tell by inspecting http://w.sharethis.com/button/buttons.js, the function call that initializes the buttons is stLight.domReady();. So I'm guessing that's what should be called in the new Drupal.behaviors.shareThis.attach() javascript function, as opposed to stButtons.locateElements().

Then you'll see stLight.domReady() calls stLight.onReady(), and the first thing that does is check if (stLight.readyRun == true) and if so, aborts from running the remainder of the function. This is why some of you are finding that the initialization only happens correctly on the first time -- because on subsequent tries, the code finds this global javascript variable saying that initialization has happened already, and it won't run it again.

Thus, I'm guessing that stLight.readyRun = false; should also be added to the new attach function, prior to calling stLight.onReady();.

As a temporary workaround: I simply stuck "<script type='text/javascript'>stLight.readyRun = false;</script>" just before my call to embed the output of sharethis_get_button_HTML(...) in my template retrieved by AJAX, and all seems well. Note that I did consider using drupal_add_js() with the 'inline' type to do this, but there is a note as to why inline js is not currently added to AJAX-retrieved output: http://api.drupal.org/api/drupal/includes!ajax.inc/function/ajax_render/7 .

klokie’s picture

My site has the same issue as #8.

RobLoach’s picture

This is very nice, and I'd really like to add it in. Is anything missing from the patch? Aside from sharethis.js ;-) .

mrbungle’s picture

Thanks tea.time for the nod in the right direction, it is appreciated. Our problem was resolved by setting stLight.readyRun to false after .getScript was called onComplete of the colorbox.

$.getScript("http://w.sharethis.com/button/buttons.js#publisher=" + share_publisher_id, function(){
stLight.readyRun = false;
});

dend01’s picture

...

langelhc’s picture

Thank you tea.time, works for me.
Read more: http://drupal.org/node/322808#comment-7357744

gmclelland’s picture

We need to change hook_init to hook_page_build see https://twitter.com/davereid/status/294554866649542657

Status: Needs review » Needs work
jrbeeman’s picture

Issue summary: View changes
FileSize
2.62 KB

Re-rolled patch against 7.x-2.5 release.

SocialNicheGuru’s picture

this no longer applies to dev version of dec/2013
no longer applies to 2.10

kenheim’s picture

What is the status of this? I tested 7.x-2.6 and had the one-time load issue with colorbox node. So I tried to apply the patch, but it failed for this 7.x-2.6 version.

Not sure what is meant by

this no longer applies to dev version of dec/2013

The last submitted patch, 4: 1197104-4-ab-sharethis-ajax.patch, failed testing.

SocialNicheGuru’s picture

the error also occurs in a view.
Add ajax to the view.
Add a filter to the view.
Add the sharethis link to a view
The first time it works fine.
select any element in the filter
the sharethis link in the view field is blank

Edit:
switching over to http://drupal.org/project/addthis for now unfortunately.

DanieleN’s picture

#2 works fine!
I use 7.x-2.12

DanieleN’s picture

Status: Needs work » Closed (fixed)
pcorbett’s picture

Status: Closed (fixed) » Needs review

Wait, I don't think this should be "closed (fixed)" just yet. Was that on purpose?

DanieleN’s picture

Nope, is not fixed. My mistake

navneet0693’s picture

Status: Needs review » Closed (outdated)
pingwin4eg’s picture

@navneet0693 Why is this outdated? Seems like the problem still persists.

pingwin4eg’s picture

Status: Closed (outdated) » Needs review
FileSize
2.12 KB

Here's my version based on previous patches that finally fixes all sides of this issue.

emb03’s picture

I am having this issue in Drupal 8.7.7 Is there a patch for D8?

apaderno’s picture

Title: ShareThis doesn't use Drupal's Ajax framework » Convert the code to use the Drupal AJAX framework