twttr.widgets.createTweet TypeError


#1

I’m getting the followiing error in all browser “TypeError: undefined is not an object (evaluating ‘twttr.widgets.createTweet’)”. Anyone know why?

I’m executing the following Javascript that was mostly copied and pasted from the Twitter developer documentation.

<script>window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return t;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);

  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };

  return t;
}(document, "script", "twitter-wjs"));</script>

<div id=demo ></div>

<script type="text/javascript" >
twttr.widgets.createTweet(
  '965403583775756288', 
  document.getElementById('demo'),
  {
    theme: 'dark'
  }
);
</script>


#2

Hi there! twttr.widgets.createTweet can only be invoked after a page has loaded, so you may try wrap that call in your document.onload event handler, e.g.

window.onload = function () {
  twttr.widgets.createTweet(
    '965403583775756288', 
    document.getElementById('demo'),
    {
      theme: 'dark'
    }
  );
  // do something else
};

#3

Thank you, I successfully tested your code change. That does make sense now that I think about it. The official documentation should include that reminder.


#4

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.