Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load twitter widget from ajax-loaded html?

I'm trying to use a pretty standard twitter search widget, straight from the twitter site:

<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'search',
  search: '$AAPL',
  interval: 6000,
  title: 'AAPL',
  subject: '',
  width: 250,
  height: 300,
  theme: {
    shell: {
      background: '#8ec1da',
      color: '#ffffff'
    },
    tweets: {
      background: '#ffffff',
      color: '#444444',
      links: '#1985b5'
    }
  },
  features: {
    scrollbar: false,
    loop: true,
    live: true,
    hashtags: true,
    timestamp: true,
    avatars: true,
    toptweets: true,
    behavior: 'default'
  }
}).render().start();
</script>

And it's getting loaded like so:

$(".linktosymbol").bind("ajax:success", function(event, data, status, xhr) {
    $(".symboldetails").html("");
    var target = $("#" + $(this).attr('data-target'));
    target.html(data);
});

It never appears though, it seems to just blank the screen and go on loading forever. Ideas?

like image 244
David N. Welton Avatar asked Jan 31 '11 13:01

David N. Welton


1 Answers

Had the same issue. The problem is that the Twitter JS creates the markup using document.write, which only works while the document is being generated. Otherwise it will create a new document.

Tip: While debugging, it's easier to use the original code at http://twitter.com/javascripts/widgets/widget.js.

<div id="twtr-widget"></div>
<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 4,
  interval: 6000,
  width: 500,
  height: 300,
  id: 'twtr-widget',
  theme: {
    shell: {
      background: '#333333',
      color: '#ffffff'
    },
    tweets: {
      background: '#000000',
      color: '#ffffff',
      links: '#4aed05'
    }
  },
  features: {
    scrollbar: false,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser('Znarkus').start();
</script>

That is my solution. Add the id parameter, and Twitter will add the markup to the element with that id (that's what the top div is for).

Note: I had problems with the minified version of their script, so I used http://twitter.com/javascripts/widgets/widget.js instead.

like image 197
Znarkus Avatar answered Nov 06 '22 07:11

Znarkus