I initialize the tweet button at the beginning of my app, after user interaction the current window's location is updated using HTML5 pushState, but the twitter button is still sharing the previous URL from when it was initialized.
How do I update the URL that twitter uses?
I figured this out. Here's how to make this work.
The general idea is:
class
of your<a>
for the twitter share button to be something other than.twitter-share-button
. Also give the<a>
astyle="display:none;"
.<div>
(or<span>
) with a uniqueid
.the hidden, mis-named,
` tag from step #1data-url
etc... attributes as you'd likestyle
attribute from the clone (unhide it)class
of the clone totwitter-share-button
append()
this clone to your<div>
from step 2.$.getScript()
to load and run Twitter's Javascript. This will convert your cloned<a>
into an<iframe>
with all the right goop.Here's my HTML (in Jade):
Then in your client-side .js:
I got the main clues for this solution from here: http://tumblr.christophercamps.com/post/4072517874/dynamic-tweet-button-text-using-jquery
I had success using the new Twitter feature that reloads the share url.
My initial HTML just has a container for the share link, like this:
Any time I get new data from an ajax call I just call updateTwitterValues() and pass along the new info. More info here https://dev.twitter.com/discussions/5642
twttr.widgets.load();
From Twitter's Dev Docs.