JavaScript Simplest Twitter Fan-gate / follower gate

Although this method allows you to asynchronously load the twitter library, but you need to make sure to setup the load before attempting to create the event handlers. Otherwise, you'll be greeted with a twttr undefined error.

Once loaded, the ready handler will fire which, in turn, allows us to setup the follow and unfollow handlers.


window.twttr = (function (d,s,id) { var t, js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } }); }(document, "script", "twitter-wjs"));
 
twttr.ready(function (twttr) {
	twttr.events.bind('follow', function(event) {
		// open up the gate!
		// reveal content, enable fields, etc., etc.
	});
	twttr.events.bind('unfollow', function(event) { 
		// close the gate!
	});
});		
Posted by
Snippet Viewed 1587 times.

Share your JavaScript code snippets:

  • Get some recognition & a link back to your site.
  • Create your own code library.
  • Help your fellow developers, as they have helped you.

 

Submit | Browse

Most Recent javascript snippets

Most Viewed