Tech Note: Creating Tweet Buttons Dynamically

LGF • Views: 8,538

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 bottom

28 comments
1 Daniel Ballard  Sun, Oct 10, 2010 1:12:42pm

Do you think you will put the new button in the pages?

2 Charles Johnson  Sun, Oct 10, 2010 1:14:55pm

re: #1 Rightwingconspirator

Do you think you will put the new button in the pages?

I'm planning to, but I need to rearrange the ratings buttons and the other buttons. I'll probably end up making the LGF Pages look exactly like the front page posts; seems like the most efficient layout.

3 _RememberTonyC  Sun, Oct 10, 2010 1:20:13pm

Is this an open football thread? Check out the pieces in the ny post on antonio cromartie and Brett Favre. One has 9 kids with 8 women and the other is text-ing pix of his penis. They play each other tomorrow night when the jets play the Vikings.

4 Daniel Ballard  Sun, Oct 10, 2010 1:21:23pm

re: #2 Charles

It is, and the button works really well.

I noticed embiggen is gone from the photos with captions, which is no loss as I see it. That works better with a smaller image and then images of about 500k & up imo. The page features are really working well, so thanks again.

OT-Now if I can just find an onshore person to do some code for me... Online its all India, no thanks.

5 CSKapper  Sun, Oct 10, 2010 2:00:54pm

Cool! I used to do some coding and I loved finding little hacks to make it easier. Of course, now some climate change denier will probably accuse me of using fake codes or something like that... o_0

6 abolitionist  Sun, Oct 10, 2010 2:02:42pm

re: #5 Sheridan8

Cool! I used to do some coding and I loved finding little hacks to make it easier. Of course, now some climate change denier will probably accuse me of using fake codes or something like that... o_0

Isn't all code fake? :)

7 Charles Johnson  Sun, Oct 10, 2010 2:11:10pm

re: #4 Rightwingconspirator

It is, and the button works really well.

I noticed embiggen is gone from the photos with captions, which is no loss as I see it. That works better with a smaller image and then images of about 500k & up imo. The page features are really working well, so thanks again.

OT-Now if I can just find an onshore person to do some code for me... Online its all India, no thanks.

That 'embiggen' feature now works when you add a caption - that was a bug, now fixed.

8 freetoken  Sun, Oct 10, 2010 2:54:21pm

Esther blessed our area today with her presence:


Former Alaska Gov. Sarah Palin Visits San Diego


Sarah Palin appeared at a fundraiser in San Diego Saturday under extra security because of a reported death threat against her and slammed what she calls the "lamestream media."

The former Alaska governor and Republican vice presidential candidate went on stage at the Sheraton San Diego Hotel and Marina on Harbor Island just after 8 p.m., where she introduced Alaska Republican Senate candidate Joe Miller, the Union-Tribune reported on its website.

Ok, I'm confused. Why should we, in a place thousands of miles away from Alaska, be a campaign stop for Joe Miller?

Standing in front of a row of American flags, Palin said the "lamestream media just doesn't get it, and when they don't believe what perhaps your message is, so they want to belittle you and mock you and treat you with much disdain, you know, I think, well they can do that to me, that's fine, because I know truth."

"You know what, you can say whatever you want to say about me but I raised a combat vet and you can't take that away from me," she added.

[...]

Persecution syndrome?

So Esther drags the Joe Miller campaign to San Diego to raise money from former military... What, can't Alaskan's pay for their own Senate campaigns?

9 researchok  Sun, Oct 10, 2010 3:02:03pm

re: #8 freetoken

Esther blessed our area today with her presence:

Former Alaska Gov. Sarah Palin Visits San Diego

Ok, I'm confused. Why should we, in a place thousands of miles away from Alaska, be a campaign stop for Joe Miller?

Persecution syndrome?

So Esther drags the Joe Miller campaign to San Diego to raise money from former military... What, can't Alaskan's pay for their own Senate campaigns?

I miss Bill Buckley.

He'd have put her head on a pike.

10 Nervous Norvous  Sun, Oct 10, 2010 3:11:04pm

re: #9 researchok

I miss Bill Buckley.

He'd have put her head on a pike.

I would have liked to have seen him publicly eviscerate her of any chance at being taken seriously by any one ever again. Preferably in such a way that she felt horribly embarrassed for her bad behavior and went into hiding in perpetuity.

Unfortunately that will never happen, not only because Bill Buckley is not among the living but because She will never be that self aware.

11 PhillyPretzel  Sun, Oct 10, 2010 3:14:40pm

re: #9 researchok
I miss William F Buckley Jr too. I believe if he were alive some political happenings would not happen.

12 Dr. Shalit  Sun, Oct 10, 2010 3:20:15pm

Tech Puke -

Last night, my trusty Acer6920 puked - BIG TIME, Vista went to sleep and would NOT wake Up, even using its OWN dog food recovery discs. Got it going again under an Ubuntu Boot, so am now LINUX, alpha to omega on this machine until I can get a W-7 Disc and usable key number. So far, life uner Linux is OK. -S-

13 b_snark  Sun, Oct 10, 2010 3:38:50pm

re: #11 PhillyPretzel

I miss William F Buckley Jr too. I believe if he were alive some political happenings would not happen.

Probably, but his cough syrup tastes like shit.

14 Ojoe  Sun, Oct 10, 2010 3:47:15pm

OT

It is against my religion to purchase health insurance.

15 Vicious Babushka  Sun, Oct 10, 2010 4:04:34pm

Who kidnapped the thread and what ransom are they demanding?

16 Ojoe  Sun, Oct 10, 2010 4:09:08pm

re: #15 Alouette

It is Sunday afternoon and time for snore, snore, snore.

17 Dark_Falcon  Sun, Oct 10, 2010 4:10:03pm

re: #16 Ojoe

It is Sunday afternoon and time for snore, snore, snore.

No, most of the discussion is still downstairs.

18 Ojoe  Sun, Oct 10, 2010 4:10:30pm

re: #17 Dark_Falcon

We are taking a nap in the loft here then.

19 Kragar  Sun, Oct 10, 2010 4:10:51pm

re: #16 Ojoe

It is Sunday afternoon and time for snore, snore, snore.

I slept in till 1pm. Of course, I didn't go to bed unitl 5am.

20 Aceofwhat?  Sun, Oct 10, 2010 4:11:13pm

re: #3 _RememberTonyC

Is this an open football thread? Check out the pieces in the ny post on antonio cromartie and Brett Favre. One has 9 kids with 8 women and the other is text-ing pix of his penis. They play each other tomorrow night when the jets play the Vikings.

Is the Brett stuff for real or still 'alleged'?

21 abolitionist  Sun, Oct 10, 2010 4:14:29pm

re: #12 Dr. Shalit

Tech Puke -

Last night, my trusty Acer6920 puked - BIG TIME, Vista went to sleep and would NOT wake Up, even using its OWN dog food recovery discs. Got it going again under an Ubuntu Boot, so am now LINUX, alpha to omega on this machine until I can get a W-7 Disc and usable key number. So far, life uner Linux is OK. -S-

Sometimes removing the battery briefly along with power cable after a normal shutdown (not sleep or hibernate) resolves some problems.

Have 3 of those Acer6920/6508's, with Vista Home Premium. About a year ago, one was in a weird state, failing to startup properly. On two occasions, I swear, I saw it display a Windows XP Pro slash graphic at power on instead of the normal Vista background.

Trying for safemode, I could get it to run a self-diagnostics/repair utility, which would report that the bootup was successful, when in fact it wasn't --I'd get to the logon screen but without any mouse or keyboard functionality. This happened more than once. Eventually, I got it to run some deeper-level diagnostics/repair, and that resolved the startup issues, but I had to abuse the power button functionality to trigger that response, ie, killing power just a few seconds into boot. (That's generally a no-no, and I *don't* recommend that technique.)

Two of the 3 are running fine now, and I'm generally satisfied with Vista. Third one needs LCD replaced.

22 Dark_Falcon  Sun, Oct 10, 2010 4:16:27pm

re: #20 Aceofwhat?

Is the Brett stuff for real or still 'alleged'?

It doesn't matter much. He likely won't be punished. Football stars get a different set of rules than normal people.

23 eastwald  Sun, Oct 10, 2010 4:24:27pm

Cool stuff Charles..I remember going nearly insane in college when we learned about the whole "this" pointer reference method. Even still, it was always such a great feeling to see a project through - I'm sure it helps a little when you're actually making some money off the site too :)

24 Aceofwhat?  Sun, Oct 10, 2010 4:38:20pm

re: #22 Dark_Falcon

It doesn't matter much. He likely won't be punished. Football stars get a different set of rules than normal people.

It's weird, though, right? In some cases, they get very preferential treatment. In other cases, like a few recent DUI cases, they get hammered. Most people get a DUI, take a vacation day or two to hide the time they spend in court, and the employer is never the wiser. Pro athlete gets a DUI and can take a nice financial hit as a result. Not that i'm on their side about it, but it's definitely a different set of rules...

25 Dr. Shalit  Sun, Oct 10, 2010 4:43:44pm

re: #21 abolitionist

ABOLITIONIST -

I was OK with Vista 'til last night. Ypou ARE correct regarding a bit of XP under Vista. Re-Partitioning for "Linux Only" showed an "XP Loader" in the Windows Partition. As I do not have a legitimate licenseable copy of XP, I didn' even try it. Ubuntu 10/10/10 is supposedly released, not QUITE yet available as it is not brought up under "update manager" as yet. ( a couple of hours ago). -S-

26 Dr. Shalit  Sun, Oct 10, 2010 4:49:27pm

Couple of days ago, I did the same thing to an Acer Aspire ONE that would NOT BOOT.
Got a Samsung Optical Drive, and made it a functioning Linux Machine. The Aspire6920 was easier, it had its OWN Built-In Optical Drive, as it is a NOTEBOOK and has such hardware as standard equipment. -S-

27 CuriousLurker  Sun, Oct 10, 2010 5:38:34pm

Love the tech notes, Charles. Thanks!

28 _RememberTonyC  Sun, Oct 10, 2010 5:54:43pm

re: #20 Aceofwhat?

Is the Brett stuff for real or still 'alleged'?

Go to dead spin.com and you tell me


This article has been archived.
Comments are closed.

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: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

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
Cash.app
Recent PagesClick to refresh
The Pandemic Cost 7 Million Lives, but Talks to Prevent a Repeat Stall In late 2021, as the world reeled from the arrival of the highly contagious omicron variant of the coronavirus, representatives of almost 200 countries met - some online, some in-person in Geneva - hoping to forestall a future worldwide ...
Cheechako
3 days ago
Views: 119 • Comments: 0 • Rating: 1
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
2 weeks ago
Views: 280 • Comments: 0 • Rating: 1