Multiple fbAsyncInit's?

坚强是说给别人听的谎言 提交于 2019-12-04 15:12:44

问题


In my site I'm using asynchronous loading of the Facebook JS SDK. To actually set it up I use the standard FB.init inside of window.fbAsyncInit function.

However the issue is that in my site this function is on every single page. However when I'm in a subpage I can't directly add to the JS function due to the design of my site, I have to copy and paste the whole function and add my bits.

I don't think multiple fbAsyncInit's are possible, so whats the best way to implement this?


回答1:


You can use this instead to check if you already have a fbAsyncInit and chain it toghether in that case:

var oldCB = window.fbAsyncInit;
window.fbAsyncInit = function(){
    if(typeof oldCB === 'function'){
        oldCB();
    }
    //Do Something else here
 };



回答2:


Sometimes the facebook api can call fbAsyncInit before your second fbAsyncInit has even started. This will fix that case:

        if (window.fbAsyncInit.hasRun === true) {
            setup(); // do something
        } else {
            var oldCB = window.fbAsyncInit;
            window.fbAsyncInit = function () {
                if (typeof oldCB === 'function') {
                    oldCB();
                }
                setup(); // do something
            };
        }



回答3:


there is a "static" property in fbAsyncInit

Try below

if (window.fbAsyncInit && window.fbAsyncInit.hasRun) {
    // do sth
}


来源:https://stackoverflow.com/questions/9219785/multiple-fbasyncinits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!