Спасибо ответу Томаса, я создал класс FacebookApiLoader для этого. Вот источник, только протестированный в Firefox 3 на данный момент. Надеюсь, это кому-нибудь поможет. Для этого нужно посмотреть, есть ли на странице какие-либо api-зависимые элементы facebook и если они есть, он загрузит скрипт api facebook, вставив его перед закрывающим тегом тела. Это зависит от библиотеки PrototypeJS. Вызовите facebookApiLoader.observe () на странице, которая может потребовать API Facebook.
var FacebookApiLoader = Class.create({
initialize: function() {
this.observer = null
this.observedElement = null
},
apiDependentsVisible: function() {
if (null == this.observedElement) {
// $('facebook-login') is a div in my site that
// is display:none initially. Once it is made
// visible then the facebook api is needed.
// Replace 'facebook-login' with id relevant for your site
this.observedElement = $('facebook-login')
}
return this.observedElement.visible()
},
apiLoadCompleted: function() {
try {
return !Object.isUndefined(FB) && !Object.isUndefined(FB_RequireFeatures)
} catch (e) {
}
return false
},
initAndRequireFeatures: function() {
FB_RequireFeatures(["XFBML"],
function() {
FB.init('secret-put-your-app-value-here','/xd_receiver.html', {})
}
);
},
initFacebookConnect: function() {
new PeriodicalExecuter(function(pe) {
if (this.apiLoadCompleted()) {
this.initAndRequireFeatures()
pe.stop()
}
}.bind(this), 0.2);
},
loadApi: function() {
// Use body for facebook script as recommended in Facebook
// docs not to insert in head as some browsers have
// trouble with it
body = $$('body')[0]
// TODO use https protocol if page is secure
script = new Element('script', { 'type': 'text/javascript',
'src': 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php' })
body.appendChild(script)
this.initFacebookConnect()
},
loadApiIfRequired: function() {
if (this.apiDependentsVisible()) {
this.observer.stop()
this.loadApi()
}
},
observe: function() {
if (null == this.observer) {
this.observer = new PeriodicalExecuter(this.loadApiIfRequired.bind(this), 0.2)
}
}
});
// The FacebookApiLoader attributes are lazily loaded
// so creating a new facebookApiLoader
// is as low resource usage as possible
var facebookApiLoader = new FacebookApiLoader();
Затем на любой странице, которая может потребовать API Facebook по требованию, звоните
facebookApiLoader.observe();