'Can Twitter's embedded tweets be rendered dynamically?

Twitter's new-ish 'embedded tweets' feature is nice, but as far as I can tell the Twitter API's widgets.js can only render the embedded tweets at page load.

The doc page for embedded tweets is here: Embedded Tweets | Twitter Developers

It describes an oembed endpoint for "dynamically rendering" a tweet, but this only returns the bare HTML code that must still be transformed by widgets.js.

If I dynamically load some content via Ajax and insert it into the DOM, and that content includes the code for an embedded tweet, is there a way to get widgets.js to render that embedded tweet dynamically? A function or method call maybe?



Solution 1:[1]

It's possible to do by calling twttr.widgets.load(). Pass in an HTMLElement as the first argument to limit the search for uninitialized widgets to that element. For example:

twttr.widgets.load(document.getElementById("container"))

Source: https://developer.twitter.com/en/docs/twitter-for-websites/javascript-api/guides/scripting-loading-and-initialization

Solution 2:[2]

The link(s) to the docs referenced above lead to blank pages, so here it is again: https://developer.twitter.com/en/docs/twitter-for-websites/embedded-tweets/overview.html

Scroll all the way down to the Render a Tweet with JavaScript section.

Pass your tweet to twttr.widgets.load like so:

twttr.widgets.load(${tweet.html})

Solution 3:[3]

I'm doing this in jQuery by doing the following:

$.getScript ( '//platform.twitter.com/widgets.js', callBack );

The callback function fires when the script has finished loading.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Diana E.
Solution 3 tbraun89