Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#31 | sharethis-stbuttons-reapplying-1197104-31.patch | 2.12 KB | pingwin4eg |
Comments
Comment #1
jvalencia CreditAttribution: jvalencia commentedCan you be a little more specific? I'm not too familiar with either Colorbox views or ajax in Drupal.
Comment #2
adamdicarlo CreditAttribution: adamdicarlo commentedJavaScript 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:
Comment #3
jvalencia CreditAttribution: jvalencia commentedSo, 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?
Comment #4
alanburke CreditAttribution: alanburke commentedI'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!
Comment #5
TravisWeihermuller CreditAttribution: TravisWeihermuller commentedI 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.
Comment #6
alanburke CreditAttribution: alanburke commentedI'm not sure its the same issue - in my case the buttons were displayed, but the Javascript didn't work.
Comment #7
andyg5000I 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.
Comment #8
billp44ruby CreditAttribution: billp44ruby commentedI 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.
Comment #9
tea.time CreditAttribution: tea.time commentedI 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 newDrupal.behaviors.shareThis.attach()
javascript function, as opposed tostButtons.locateElements()
.Then you'll see
stLight.domReady()
callsstLight.onReady()
, and the first thing that does is checkif (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 callingstLight.onReady();
.As a temporary workaround: I simply stuck
"<script type='text/javascript'>stLight.readyRun = false;</script>"
just before my call to embed the output ofsharethis_get_button_HTML(...)
in my template retrieved by AJAX, and all seems well. Note that I did consider usingdrupal_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 .Comment #10
klokie CreditAttribution: klokie commentedMy site has the same issue as #8.
Comment #11
RobLoachThis is very nice, and I'd really like to add it in. Is anything missing from the patch? Aside from sharethis.js ;-) .
Comment #12
mrbungle CreditAttribution: mrbungle commentedThanks 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;
});
Comment #13
dend01 CreditAttribution: dend01 commented...
Comment #14
langelhc CreditAttribution: langelhc commentedThank you tea.time, works for me.
Read more: http://drupal.org/node/322808#comment-7357744
Comment #15
gmclelland CreditAttribution: gmclelland commentedWe need to change hook_init to hook_page_build see https://twitter.com/davereid/status/294554866649542657
Comment #17
jrbeemanRe-rolled patch against 7.x-2.5 release.
Comment #18
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedthis no longer applies to dev version of dec/2013
no longer applies to 2.10
Comment #19
kenheim CreditAttribution: kenheim commentedWhat 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
Comment #22
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedthe 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.
Comment #25
DanieleN CreditAttribution: DanieleN commented#2 works fine!
I use 7.x-2.12
Comment #26
DanieleN CreditAttribution: DanieleN commentedComment #27
pcorbett CreditAttribution: pcorbett at Redfin Solutions, LLC commentedWait, I don't think this should be "closed (fixed)" just yet. Was that on purpose?
Comment #28
DanieleN CreditAttribution: DanieleN commentedNope, is not fixed. My mistake
Comment #29
navneet0693 CreditAttribution: navneet0693 as a volunteer and at QED42 commentedComment #30
pingwin4eg@navneet0693 Why is this outdated? Seems like the problem still persists.
Comment #31
pingwin4egHere's my version based on previous patches that finally fixes all sides of this issue.
Comment #32
emb03 CreditAttribution: emb03 commentedI am having this issue in Drupal 8.7.7 Is there a patch for D8?
Comment #33
apaderno