Wednesday, June 1, 2011

[twitter-dev] Re: Introducing the Follow Button

> - Detailed documentation:http://dev.twitter.com/pages/follow_button

That page recommends the following code snippet:

<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')
[0].appendChild(twitterWidgets);
})();
</script>

This could be optimized into:

<script>
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
(document.head || document.getElementsByTagName('head')
[0]).appendChild(twitterWidgets);
})();
</script>

Most of the optimizations I applied are explained in detail here:
http://mathiasbynens.be/notes/async-analytics-snippet

Note that the `.async=true` is only useful for Firefox 3.6 – in every
other browser, it doesn't make a difference (as per the spec). You
could consider removing that as well in the near future. See
http://mathiasbynens.be/notes/async-analytics-snippet#async for more
information.

If you're gonna append to the <head> anyway, you might as well use
`document.head` if it's available: http://mathiasbynens.be/notes/document-head
However, a more robust cross-browser solution would be to insert the
dynamic script after or before the first <script>, like Google does
with its GA snippet.

--
Twitter developer documentation and resources: https://dev.twitter.com/doc
API updates via Twitter: https://twitter.com/twitterapi
Issues/Enhancements
Tracker: https://code.google.com/p/twitter-api/issues/list
Change your membership to this group: https://groups.google.com/forum/#!forum/twitter-development-talk

No comments:

Post a Comment