Embedded Moment JavaScript Factory Function
Twitter’s widget JavaScript library supports dynamic insertion of a Twitter Moment using the twttr.widgets.createMoment function. Pass a Moment ID, target container element, and an optional options object to insert an embedded Moment into your page.
The code snippets on this page assume Twitter’s widgets.js has successfully loaded on your page. Include an asynchronous script loader on your page while initializing window.twttr as described in our JavaScript loader documentation. All JavaScript code depending on widgets.js should execute on or after twttr.ready.
Arguments
A Twitter Moment identifier.
Example Values: '650667182356082688'
DOM node of the desired insertion point.
Example Values: document.getElementById('container')
Override default widget options.
Example Values: { limit: 3 }
Example
An element with a DOM ID of container exists on the page.
<div id="container"></div>
The code snippet below will create a embedded Moment for Moment ID “650667182356082688” inserted into a page inside an element with a unique ID of container.
twttr.widgets.createMoment(
'650667182356082688',
document.getElementById('container'),
{
limit: 3
}
);Promises
The twttr.widgets.createMoment function returns a Promise. You can execute code after a widget has been inserted onto your page by passing a callback to the resulting promise’s then function.
twttr.widgets.createMoment(...)
.then( function( el ) {
console.log('Moment added.');
});