Anders Brownworth

Technology and Disruption

jQuery: Change or Reload an iframe for Twitter Buttons

The standard javascript Twitter buttons execute their script on page load and then never again. You are out of luck if you want to programmatically change parameters later. (such as setting the "URL to tweet") The work-around is to instead use the iframe version of Twitter buttons and use query parameters to set options.

<iframe id="twitFrame" allowtransparency="true" frameborder="0" scrolling="no" src="//platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fanders.com%2F&count=none" style="width:130px; height:20px;"></iframe>

Then change the iframe's src programmatically later to set options on the fly via jQuery. Here I change the URL to http://example.com/.

$( '#twitFrame' ).attr( 'src', '//platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fexample.com%2F&count=none' );

Comments (0)

Leave a Comment

Name:
Location: (city / state / country)
Email: (not published / no spam)
Comment:

No HTML is allowed. Cookies must be enabled to post. Your comment will appear on this page after a moderator OKs it. Offensive content will not be published.

Click the banana to submit your comment.

To create links in comments:
[link:http://anders.com/] becomes http://anders.com/
[link:http://anders.com/|Anders.com] becomes Anders.com
Notice there is no rel="nofollow" in these hrefs. Links in comments will carry page rank from this site so only link to things worthy of people's attention.