Ondemand loading of widgets.js?


#1

I could not find how to implement on demand loading of widgets.js, which would require a callback.

widgets.js?jsonp=callback

Anything? Or is it not supported?


#2

Can you give a little more info about what you mean by ‘on demand’ loading?

You can use twttr.ready() to execute code after the script is ready, and you can subscribe to the loaded event like this:

twttr.events.bind(‘loaded’, function (e) {
// widgets are ready
}

B


#3

On demand loading refers to loading in response to a user action, such as a click, and not on load or document ready.


#4

I see. To do this, you should extract a little piece from the default embed code and include that in your page/script, which means you can put scripts and callbacks in place that will get picked up when the script gets loaded later:

window.twttr = window.twttr || { _e: [], ready: function (f) { t._e.push(f) } };

That puts the placeholder functions in place that widgets.js will pick up when it loads later.

Then, elsewhere in your code:

twttr.ready(function (t) { // If you want to respond to widgets that initialize: t.events.bind('loaded', function (elements) { // then .. });
// Or if you want to create a widget
t.widgets.createTweet('212', document.body)
    .then(function () {
        // etc
    });

});

Let me know how you get on.

Ben


#5