Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple fbAsyncInit's?

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?

like image 890
TheLQ Avatar asked Feb 22 '23 13:02

TheLQ


2 Answers

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
 };
like image 185
Eduardo Avatar answered Mar 03 '23 08:03

Eduardo


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
            };
        }
like image 31
Kim T Avatar answered Mar 03 '23 09:03

Kim T