Google+ allows developers to utilize what they call an “explicit render” to display the Google+ button only when a particular event takes place on the page (see: http://code.google.com/apis/+1button/#example-explicit-render). This is very helpful on pages where multiple buttons are required.
Twitter, however, renders a tweet button (using an iframe) in the place of each link on the page with the class “twitter-share-button”. This causes very slow page loads (even when I load Twitter’s widget.js asynchronously after the DOM is loaded.
Does anyone know of a way to mimic the Google+ explicit render for Tweet buttons, or a way to render a Tweet button only when a certain DOM event takes place (like mouseover)?
I finally found a way to do this. It’s not nearly as clean as the Google+ button, but here it is:
First, make sure you have jQuery included in your document’s head:
Then create a link that has the same URL as the eventual tweet button, along with an empty div where your button will be rendered:
At the bottom of your page, right above the /body tag, include the javascript from Twitter and the function that will render the button, as well as the listener that will activate the function when the desired event takes place:
Lastly, add a link to your page somewhere that will activate the function above based on some event:
That’s it.
If you want the entire test HTML page to see how I’ve got it working, see here: