Tech Note: Creating Tweet Buttons Dynamically

LGF • Views: 6,989

As I mentioned yesterday, I’ve integrated Twitter’s new official “Tweet Button” into the LGF Blog code to see how it compares to our previous Tweet button/retweet count feature, which calls the API at bit.ly to get the count statistics.

But I ran into a problem with the Twitter code: it’s intended to work only on page load, and there’s no documented way to dynamically add Tweet Buttons to content generated via Ajax. This meant I couldn’t add Tweet Buttons to the articles that appear when you click the “Show More Articles” button at the bottom of the LGF front page.

So I took a look at Twitter’s Javascript code for generating the buttons, which is located here. It’s “minified,” so I ran it through the Javascript unpacker and beautifier first, then read through the code (it’s pretty short) and discovered that installing a Tweet Button is a simple matter of instantiating a “TweetButton” object (passing it the DOM object — not the jQuery object — for the link that will be turned into a button), then calling the TweetButton’s “render” method.

The code to do this dynamically via jQuery is surprisingly simple. (The variable “thediv” is the container for the newly loaded articles.)

$(thediv).find('a.twitter-share-button').each(function(i) {
	var thisTweetButton = new twttr.TweetButton($(this).get(0));
	thisTweetButton.render();
});

A sidenote: I’ve noticed that the official Tweet Button is reporting lower numbers for the retweet count than bit.ly’s API does. It’s not immediately apparent why there’s a discrepancy.

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2016-01-01 10:29 am PST
LGF User's Guide RSS Feeds Tweet

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Square Cash Shop at amazon
as an LGF Associate!
Recent PagesClick to refresh
How Sigmund Freud Tried to Break and Remake His Fiancée The area in which Martha most urgently needed reeducation, Freud believed, was that of excessive regard for her own family. He had coveted its name before their engagement, but now the very illustriousness of Martha’s connections prompted a worry ...
Birth Control Works
8 hours, 43 minutes ago
Views: 317 • Comments: 0 • Rating: 1
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Ten Sailors Missing After U.S. Warship, Tanker Collide Near Singapore SINGAPORE (Reuters) - Ten sailors are missing after a U.S. warship collided with an oil tanker east of Singapore before dawn on Monday, tearing a hole beneath the waterline and flooding compartments that include a crew sleeping area, the ...
Thanos
1 day, 13 hours ago
Views: 250 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Why You Can’t Take a Flag to a National Cemetery Why You Can't Take a Flag to Arlington (or other national veteran cemeteries) Stars and Stripes, reprinting with permission an article from the Washington Post, explains why you cannot take a flag to Arlington National Cemetery (or any other ...
Anymouse 🌹
3 days, 20 hours ago
Views: 417 • Comments: 0 • Rating: 2
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Another Democrat Starting Trump Impeachment Process Come on guys, get behind the bill. All of us. we should get behind this bill as it emerges as fast as we attacked the BCA. Following Donald Trump’s equivocating of white supremacists and those who oppose them in ...
Unshaken Defiance
5 days, 5 hours ago
Views: 630 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 1
Comments: 0
: 1
How Militias Became the Private Police for White Supremacists Magazine Guns to a protest? Clubs? We can not tolerate anti American militias. The bloody weekend in Charlottesville that began with several hundred chanting white supremacists on a torch-lit march will long be remembered for the searing image of a ...
Unshaken Defiance
5 days, 6 hours ago
Views: 641 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Starting in 2018, Police Dogs Injured in the Line of Duty Can Be Transported by EMS to Veterinary Clinics ST. CLAIR COUNTY, IL – Ambulances are for people. It’s always been the law of the land. In Illinois, it'll soon be "almost always". Illinois Governor, Bruce Rauner, signed House Bill 2661, Tuesday. It will allow police dogs to ...
Birth Control Works
5 days, 9 hours ago
Views: 647 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
The New War on Birth Control - Pacific Standard Although it wasn't clear at the time, the attack on Phillips marked the beginning of a new anti-contraception movement—conceived in the U.S., but unfolding in Africa, where women's frustrations with the dearth of safe, effective family planning options are ...
Birth Control Works
5 days, 9 hours ago
Views: 688 • Comments: 0 • Rating: 1
Tweets: 1 • Share to Facebook
Shares: 0
Comments: 0
: 0
M83 - Do It, Try It (David Wilson Video) M83 - Do It, Try It video by David Wilson. Following the video's premiere at Sundance Next Fest it is now available to watch online. The track is taken from M83's seventh studio album Junk, available to buy & ...
Thanos
5 days, 23 hours ago
Views: 566 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
Marvel’s the Defenders It’s all coming together. Learn about The Defenders from the stars themselves: Charlie Cox, Krysten Ritter, Mike Colter and Finn Jones. Marvel’s The Defenders premieres globally August 18, 2017, only on Netflix. Watch The Defenders on Netflix: netflix.com SUBSCRIBE: ...
Thanos
6 days ago
Views: 601 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0
AG Madigan Issues Guidelines for Responding to Sexual Assault Crimes “We took a comprehensive look at why sexual assault survivors rarely report their crimes to law enforcement, and it was clear that our criminal justice system can and must respond, investigate and better support survivors,” Illinois Attorney General Lisa ...
Birth Control Works
6 days, 5 hours ago
Views: 556 • Comments: 0 • Rating: 0
Tweets: 0 • Share to Facebook
Shares: 0
Comments: 0
: 0