title: Twitter Embedded Timeline Callback
body: <p>I've been doing a lot of research into Twitter's Embedded Timelines. I've been trying to figure out if it is possible to know when the timeline is finished loading and <em>displays</em> on the page. This <a href="https://dev.twitter.com/discussions/13264" rel="noreferrer">issue has been requested</a>, but has yet to be implemented. I have come to a dead end an am hoping someone is able to help me find the solution. Here is what I have found so far:</p>
  
  <p>The code to add an Embedded Timeline:</p>
  
  <pre><code>&lt;a class="twitter-timeline" href="https://twitter.com/twitterapi" data-widget-id="YOUR-    WIDGET-ID-HERE"&gt;Tweets by @twitterapi&lt;/a&gt;
  &lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;
  </code></pre>
  
  <p>What happens when the script is run:</p>
  
  <ol>
  <li><p>The script replaces the <code>&lt;a&gt;</code> tag with an <code>&lt;iframe&gt;</code> element.</p></li>
  <li><p>The contents of the <code>iframe</code> are ready, but the avatar image and the 'follow @username' button are not done downloading. The <code>iframe</code>'s <code>height</code> attribute is set to 0 so that you don't see the content without the images.</p></li>
  <li><p>The avatar image is downloaded, but the 'follow @username' button is still downloading. Content is still not visible.</p></li>
  <li><p>The 'follow @username' button is finished downloading. The <code>iframe</code>'s <code>height</code> attribute is set to match the height of the contents of the <code>iframe</code>. The timeline is now visible.</p></li>
  </ol>
  
  <p>Now, I've tried everything I can think of to figure out when the timeline is visible (<em>without</em> using settimeout/interval). The goal is to have a non-polling event/function/callback. Here is what I have tried so far without success:</p>
  
  <ul>
  <li>Setting event listeners on the <code>iframe</code> (such as <code>onload</code>, <code>DOMContentLoaded</code>, <code>DOMFrameContentLoaded</code>, etc) to know when the content has finished loading. This doens't work becuase the <code>iframe</code> has no <code>src</code> attribute. After going through the uglified code (I was unable to find an uncompressed version), my guess is that it is using writes to add the content to the body of the <code>iframe</code>.</li>
  <li>Setting event listeners to know when the <code>height</code> attribute changes on the <code>iframe</code>. Theoretically, this event should fire three times: first when the content loads, second when the <code>height</code> is set to 0, and third when the <code>height</code> is set to show the fully loaded timeline. However, I was only able to get the event to fire for the first two cases and never for the third (the one I actually wanted). I used <code>DOMSubtreeModified</code>, <code>onreadystatechange</code>, and <code>onpropertychange</code>, but only <code>DOMSubtreeModified</code> worked for firing the event.</li>
  <li>Setting event listeners on the cotents of the <code>iframe</code>. Because JavaScript can reach inside of the <code>iframe</code> from Twitter, it is possible to grab any element inside the <code>iframe</code> (such as <code>document</code> or <code>window</code>). However, adding <code>onload</code>, <code>DOMContentLoaded</code>, or <code>DOMFrameContentLoaded</code> event listeners on the <code>window</code> or <code>document</code> of the <code>iframe</code> still does not fire those events.</li>
  <li>Setting event listeners on the 'follow @username' button. The button is actually an <code>iframe</code> that does have a <code>src</code> attribute. By setting an <code>onload</code> event, it will get fired when it is loaded. However, the <code>onload</code> event will always fire twice. The first time when it has finished downloading and the second when it has finished processing. Immediatly after the second trigger, the timeline will be shown. To implement this is quite the hack though (I guess everything is) since you must wait for the contents of the <code>iframe</code> to load, reach inside and get the 'follow @username' <code>iframe</code>, set an <code>onload</code> event on it, then wait for the second event to fire.</li>
  <li>Using the <code>twttr.widgets.createTimeline()</code> function, which has an optional parameter for a callback. However, the callback is called when the content is ready (step 2), but not when the iframe is visible (step 4).</li>
  </ul>
  
  <p>There's probably a few more combinations I forgot at this point. I know there exists a <a href="https://gist.github.com/johan/4479186" rel="noreferrer">github gist</a> that uses the Twitter <code>widget.js</code>, but adds callbacks to it. I was unable to get this to work.</p>
  
  <p>Sorry for the log question, but I hope someone is able to help. Thanks.</p>