Embedded Video JavaScript Factory Function
Twitter’s widget JavaScript library supports dynamic insertion of Embedded Videos using the twttr.widgets.createVideo function. Pass the video’s Tweet ID, target parent element, and any custom options.
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 twitter.ready.
Arguments
The numerical ID of the Tweet containing the original video.
Example Value: '560070183650213889'
DOM node of the desired insertion point.
Example Value: document.getElementById('container')
Override default widget options. See Embedded Video parameter reference for details.
Example Value: { status: 'hidden' }
Example
An element with a DOM ID of container exists on the page.
<div id="container"></div>
The code snippet below will insert the video from Tweet ID 560070183650213889 into a page inside an element with a unique ID of container. The options object specifies a a hidden (direct link) Tweet configuration.
twttr.widgets.createVideo(
'560070183650213889',
document.getElementbyId('container'),
{
status: 'hidden'
}
);
Promises
The twttr.widgets.createVideo 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.createVideo(...)
.then( function( el ) {
console.log('Video added.');
});