Joe
Joe

Reputation: 1110

Twitter Uncaught TypeError: undefined is not a function

I have narrowed down the error I am having with my Twitter widget usage to the binding of the event.

twttr.events.bind('tweet', function (event) { addShared(); });

or

    twttr.events.bind('tweet', addShared);

seem to produce the same error: Uncaught TypeError: undefined is not a function. They both ultimately work but the button acts funny from time to time and I'm wondering if this error has anything to do with the problem.

Note, the error shows up in Chrome when inspecting the webpage before the button is ever clicked but the button works and the tweets work but the resulting bound function does not work anymore. This is a problem that I recently noticed without having changed the twitter code at all and the event binding was previously functional.

Any advice or even acknowledgement of having seen the same problem would be nice, thanks.

Upvotes: 3

Views: 7265

Answers (1)

Joe
Joe

Reputation: 1110

The issue ended up being solved on Twitter's discussions by @indianburger.

The problem was how the code was being loaded and called. The solution came in the form of a jsfiddle: @indianburer's jsfiddle.

If that is broken, the simplest example of what is needed for event binding is:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="https://twitter.com/share" data-via="josephjohnbless">Tweet</a>
<script>
  window.twttr = (function (d,s,id) {
      var t, js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
      js.src="https://platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js, fjs);
      return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
  }(document, "script", "twitter-wjs"));
</script>
<script>
    twttr.ready(function (twttr) {
        twttr.events.bind('click', function (event) { alert('yes'); });
    });
</script>

For reference, this is the Twitter discussion link.

Upvotes: 6

Related Questions