Intermittent iFrame Height

464 views Asked by At

I have this code:

<script type="text/javascript">  
     window.fbAsyncInit = function() { 
     FB.Canvas.setSize({ height: 6000 }); 
} 
window.fbAsyncInit();   
</script>

This resizes my iFrame correctly about 50% of the time. The other 50% of the time it does not resize and the following error appears on my console:

Uncaught ReferenceError: FB is not defined
window.fbAsyncInit:122
(anonymous function)

At first I thought this was an error with how I embedded the Facebook Javascript SDK, but then why does it work sometimes and not at other times?

1

There are 1 answers

2
Shadow Wizard Love Zelda On BEST ANSWER

Might be "race condition" of some sort, so just wait until FB is defined:

window.fbAsyncInit = function FbAsynchInit() { 
     if (typeof FB != "undefined" && FB) {
         FB.Canvas.setSize({ height: 6000 }); 
     } else {
         window.setTimeout(FbAsynchInit, 10);
     }
} 

When FB won't be defined it will keep checking every 10 milliseconds, until it's defined.