3

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.

Joe
  • 1,080
  • 1
  • 14
  • 20
  • 1
    The first argument in `bind()` if this is the native `bind()`, is the value of `this` inside the function, I doubt you want to set that to the string `tweet`, how about trying `twttr.events.bind(twttr, 'tweet', addShared);` – adeneo Jul 10 '14 at 18:38
  • Thanks for trying but that didn't seem to affect the error at all. I'm continuing to play around with the code to see if I can come up with anything but it looks like the usage I have is the same as what a lot of people are doing: http://stackoverflow.com/questions/19236039/get-data-attr-when-twitter-button-is-clicked and what I'm doing follows their documentation: https://dev.twitter.com/docs/tfw/events – Joe Jul 10 '14 at 19:51
  • I was getting the same error because the html portion of the script was inside a
    . Try putting it outside of the form and the issue will be solved.
    – Andres SK Nov 06 '15 at 05:50
  • It seems like most of our websites use .Net web forms and that may not be an option with our current setup. I will have to give this a try sometime after restructuring some of the site. In the meantime, my answer worked for the problem but you can post your comment as an answer for those who might benefit from it. – Joe Nov 12 '15 at 16:17

1 Answers1

6

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.

Joe
  • 1,080
  • 1
  • 14
  • 20