I have a website using infinite scroll to display node teasers on the homepage. Each teaser has a facebook/google+ button. On the initial page load the social buttons load just fine. However when you scroll, the new teasers that load never have the buttons initialized and that space is just left blank.

Live site:
http://matttucker.onedogdevelopment.com/

Annotated screenshot:
http://www.diigo.com/item/image/1ks4o/61p8

I'm not sure if this is a bug report or support request. Please correct accordingly.

CommentFileSizeAuthor
#10 infinite scroll.png523.17 KBquickly

Comments

bryancasler’s picture

Title: How can I initiate facebook/google+ buttons for view element loaded after scrolling. » How can I initiate facebook/google+ buttons for view's elements loaded after scrolling?

Grammar correction and I also wanted to mention I'm using the current git snapshot, so all the D7 commits today have been included.

Remon’s picture

Status: Active » Postponed (maintainer needs more info)

@animelion,

I'm not sure if your using a contrib or a custom functionality to add googleplus button. Either way, it should be fine as long as your js code is wrapped in a Drupal js behavior that gets executed when Drupal.attachBehaviors() is called.

Views Infinite Scroll module calls Drupal.attachBehaviors on newly loaded content, i.e if you're js code, that is responsible for adding the button is actually a behavior, then everything should be fine.
Hope that helps.

Feel free to re-open if I'm not making myself clear enough.

bryancasler’s picture

Hi Remon,

I tried several contrib modules that all resulted in the same effect I described above...
http://drupal.org/project/addthis
http://drupal.org/project/fb_social
http://drupal.org/project/sharethis
http://drupal.org/project/google_plusone

So then I decided to try a more direct approach. I inserted the button code directly via a Panel's "Custom Content" pane in the node teaser.

HTML

<div class="g-plusone" data-annotation="inline" data-href="%node:url"></div>
<div class="fb-like" data-href="%node:url" data-send="false" data-width="300" data-show-faces="false"></div>

Javascript

jQuery(document).ready(function($){

	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();
	  
	(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) return;
		js = d.createElement(s); js.id = id;
		js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=157084604417019";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));

});

This custom configuration is what you currently see on the live website. I'm not proficient enough with JS to figure out what I'm supposed to do with "Drupal.attachBehaviors". I've been googling around to see if I can find any clues, but any help would be greatly appreciated.

REF: #983738: Call Drupal.attachBehaviors() after a load of new content
REF: http://drupal.org/node/114774#javascript-behaviors

bryancasler’s picture

Ok, thanks to help from mkadin in the #drupal-support IRC room I am now using this JS

Drupal.behaviors.socialbuttons = {
	attach: function(context, settings) {
		(function() {
			var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
		})();
	  
		(function(d, s, id) {
			var js, fjs = d.getElementsByTagName(s)[0];
			if (d.getElementById(id)) return;
			js = d.createElement(s); js.id = id;
			js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=157084604417019";
			fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));	 
	}
 };

The only problem remaining is that the Facebook buttons still don't load on infinite scroll. These changes are live and you can see them here. http://matttucker.onedogdevelopment.com/

bryancasler’s picture

Alright, I narrowed it down further. The reason for FB not loading has something to do with the code not being asynchronous . So I googled for "Facebook Like Button asynchronous " and got some jquery code (link: http://daily.siebler.eu/2011/04/facebook-like-button-asynchronous-loadin...). Spliced that suggestion in with my existing Facebook function and bam! everything works.

Working JS

Drupal.behaviors.socialbuttons = {
	attach: function(context, settings) {
		(function() {
			var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
		})();

// Old Facebook Code	  
//		(function(d, s, id) {
//			var js, fjs = d.getElementsByTagName(s)[0];
//			if (d.getElementById(id)) return;
//			js = d.createElement(s); js.id = id;
//			js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=157084604417019";
//			fjs.parentNode.insertBefore(js, fjs);
//		}(document, 'script', 'facebook-jssdk'));	 

//Asynchronous Facebook Code
//Source: http://daily.siebler.eu/2011/04/facebook-like-button-asynchronous-loading-with-jquery/		
		(function() {
			// prevent jQuery from appending cache busting string to the end of the FeatureLoader URL
			var cache = jQuery.ajaxSettings.cache;
			jQuery.ajaxSettings.cache = true;
			// Load FeatureLoader asynchronously. Once loaded, we execute Facebook init 

			jQuery.getScript('http://connect.facebook.net/en_US/all.js#xfbml=1&appId=157084604417019', function() {
			FB.init({appId: 'your_app_id-optional', status: true, cookie: true, xfbml: true});
			});
			// just Restore jQuery caching setting
			jQuery.ajaxSettings.cache = cache;
		})();
	}
 };

The good news is that this solution also works with the LabJS module and core's JS Aggregation.

I don't think this issue is ready to be closed however. It would be great if we could figure out some solution/documentation for getting Views Infinite Scroll to work with these other social modules.

http://drupal.org/project/addthis
http://drupal.org/project/fb_social
http://drupal.org/project/sharethis
http://drupal.org/project/google_plusone

bryancasler’s picture

Ok, this solution just stopped working for some unknown reason.

quickly’s picture

I am having a similar issue... I know it is not a bug in this module, I just cannot figure out the solution... It will be nice if someone could guide me..

I am using js for giving a grayscale to colored effect on mouse hover on images using the code on:
http://webdesignerwall.com/tutorials/html5-grayscale-image-hover

I have this file in my themes .info file. It is working fine everywhere but when I use it with infinite scroll only the images loaded on page load are effected by this js file... the ones that come on scrolling are not...

can anyone guide me... I know it has something to do with drupal behaviours... But I hae no idea about attaching a behavior and stuff... I have googled alot but nothing was quite understandable for me :S

I know it has a simple solution, I just cant figure it out :(

quickly’s picture

#5 worked for me... Thanks @animelion

urlM’s picture

You can asynchronously load the tweet button too if you add this function to animelion's js in #5:

// Tweet Button
(function() {
    var twitterScriptTag = document.createElement('script');
    twitterScriptTag.type = 'text/javascript';
    twitterScriptTag.async = true;
    twitterScriptTag.src = 'http://platform.twitter.com/widgets.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(twitterScriptTag, s);
})();
quickly’s picture

StatusFileSize
new523.17 KB

I am using asynchronous loading for facebook, google+ and twitter as described in this post... In Firebug console I am seeing a separate call for every google+ and twitter widget. Is it normal or something is wrong? I am also using pinterest and facebook widgets but there is not any call in console for them (although they are working perfectly fine). I have attached the image.

Anonymous’s picture

I messed around with this for a while. Save yourself some time and use the Facebook iFrame code. :-)

mahipal46’s picture

add code in your js file: addthis.toolbox('.addthis_toolbox');

shailu29’s picture

Issue summary: View changes

For google +
Drupal.behaviors.socialbuttons = {
attach: function(context, settings) {
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
}
};

For Twitter
jQuery(document).ajaxComplete(function () {
twttr.widgets.load();
});

For Facebook

jQuery(document).ajaxComplete(function () {
FB.XFBML.parse();
});

honza pobořil’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

7.x-1.x will receive no work. Migrate to 7.x-2.x.