For best performance and reliability, include the widgets.js script in your template
Include the X for Websites JavaScript once in your page template for optimal web page performance and to enable tracking of X widget JavaScript events. If your site is using multiple widgets you can set up X widgets in your pages once, which will make your site faster, and widgets such as embedded Posts will be more reliable for authors when using content management systems.- Assign a HTML element ID of
twitter-wjsto easily identify if the JavaScript file already exists on the page. Exit early if the ID already exists. - Asynchronously load the Twitter for Websites JavaScript.
- Initialize an asynchronous function queue to hold dependent functions until the script is available.
twttr.ready asynchronous function queue.
Ignore script tags from embeds
If you include the X JavaScript loader on every page you do not need to include the<script> element generated by a button configurator, embedded Post, or embedded Timeline tool. You can delete the extra <script> HTML from generated markup.
If you use the oEmbed API to include Posts in your content, you can generate embed codes that don’t include the <script> element by passing omit_script=true in the query string.