Embedded Video JavaScript Factory Function

The Twitter for Websites 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 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

Parameter Description Example value
tweetID The numerical ID of the Tweet containing the original video. '560070183650213889'
targetEl DOM node of the desired parent element. document.getElementById('container')

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')
);

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.');
});