Twitter’s tweet embed
Recently I got interested in how Twitter enables users to embed tweets in their personal webpage. After clicking on “Embed Tweet”, a dialog pops up with a snippet of HTML you can copy and paste into your site, like so:
<blockquote class="twitter-tweet" lang="en"><p lang="en" dir="ltr">Kobe on the Blue Ridge Parkway... <a href="http://t.co/s1VoQXOVld">pic.twitter.com/s1VoQXOVld</a></p>— Gregory Garrett (@gcgarrett) <a href="https://twitter.com/gcgarrett/status/642774905612713984">September 12, 2015</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
The first bit is a standard blockquote that contains the content of the tweet, a link to the media included in the tweet (in this case, because the tweet I'm embedding has a picture of my dog), attribution to the author, a link to the tweet, and the date it was tweeted. However, it doesn't look much like the preview Twitter shows you:
Kobe on the Blue Ridge Parkway... pic.twitter.com/s1VoQXOVld
— Gregory Garrett (@gcgarrett)
September 12, 2015
vs.
And that's because of that script tag at the end. If you download the file it's pretty sizable, including a promise library even, but in essence it does something pretty straight forward: it creates an iframe tag with src set to the Twitterfied embedded tweet, and then replaces the blockquote with the iframe!
Because they're using an iframe they can then include whatever styling is necessary to give embedded tweets a uniform look across websites, and include things like the author's profile picture, buttons for liking, retweeting, following, etc, all while having very minimal markup to include on a site! And if the script fails, there's always that basic blockquote as a fallback.










